[yul-phaser] Program: Add parseObject()

This commit is contained in:
Kamil Śliwak 2020-03-11 21:15:12 +01:00
parent 148b87c977
commit 7107ef13a7
2 changed files with 45 additions and 0 deletions

View File

@ -26,6 +26,7 @@
#include <libyul/AsmJsonConverter.h> #include <libyul/AsmJsonConverter.h>
#include <libyul/AsmParser.h> #include <libyul/AsmParser.h>
#include <libyul/AsmPrinter.h> #include <libyul/AsmPrinter.h>
#include <libyul/ObjectParser.h>
#include <libyul/YulString.h> #include <libyul/YulString.h>
#include <libyul/backends/evm/EVMDialect.h> #include <libyul/backends/evm/EVMDialect.h>
#include <libyul/optimiser/Disambiguator.h> #include <libyul/optimiser/Disambiguator.h>
@ -136,6 +137,46 @@ variant<unique_ptr<Block>, ErrorList> Program::parseSource(Dialect const& _diale
return variant<unique_ptr<Block>, ErrorList>(move(ast)); return variant<unique_ptr<Block>, ErrorList>(move(ast));
} }
variant<unique_ptr<Block>, ErrorList> Program::parseObject(Dialect const& _dialect, CharStream _source)
{
ErrorList errors;
ErrorReporter errorReporter(errors);
auto scanner = make_shared<Scanner>(move(_source));
ObjectParser parser(errorReporter, _dialect);
shared_ptr<Object> object = parser.parse(scanner, false);
if (object == nullptr || !errorReporter.errors().empty())
// NOTE: It's possible to get errors even if the returned object is non-null.
// For example when there are errors in a nested object.
return errors;
Object* deployedObject = nullptr;
if (object->subObjects.size() > 0)
for (auto& subObject: object->subObjects)
// solc --ir produces an object with a subobject of the same name as the outer object
// but suffixed with "_deployed".
// The other object references the nested one which makes analysis fail. Below we try to
// extract just the nested one for that reason. This is just a heuristic. If there's no
// subobject with such a suffix we fall back to accepting the whole object as is.
if (subObject != nullptr && subObject->name.str() == object->name.str() + "_deployed")
{
deployedObject = dynamic_cast<Object*>(subObject.get());
if (deployedObject != nullptr)
break;
}
Object* selectedObject = (deployedObject != nullptr ? deployedObject : object.get());
// NOTE: I'm making a copy of the whole AST to get unique_ptr rather than shared_ptr.
// This is a slight performance hit but it's much less than the parsing itself.
// unique_ptr lets me be sure that two Program instances can never share the AST by mistake.
// The public API of the class does not provide access to the smart pointer so it won't be hard
// to switch to shared_ptr if the copying turns out to be an issue (though it would be better
// to refactor ObjectParser and Object to use unique_ptr instead).
auto astCopy = make_unique<Block>(get<Block>(ASTCopier{}(*selectedObject->code)));
return variant<unique_ptr<Block>, ErrorList>(move(astCopy));
}
variant<unique_ptr<AsmAnalysisInfo>, ErrorList> Program::analyzeAST(Dialect const& _dialect, Block const& _ast) variant<unique_ptr<AsmAnalysisInfo>, ErrorList> Program::analyzeAST(Dialect const& _dialect, Block const& _ast)
{ {
ErrorList errors; ErrorList errors;

View File

@ -98,6 +98,10 @@ private:
yul::Dialect const& _dialect, yul::Dialect const& _dialect,
langutil::CharStream _source langutil::CharStream _source
); );
static std::variant<std::unique_ptr<yul::Block>, langutil::ErrorList> parseObject(
yul::Dialect const& _dialect,
langutil::CharStream _source
);
static std::variant<std::unique_ptr<yul::AsmAnalysisInfo>, langutil::ErrorList> analyzeAST( static std::variant<std::unique_ptr<yul::AsmAnalysisInfo>, langutil::ErrorList> analyzeAST(
yul::Dialect const& _dialect, yul::Dialect const& _dialect,
yul::Block const& _ast yul::Block const& _ast