From 9012ade27ff653c96ef2d9e3a846561cac03d827 Mon Sep 17 00:00:00 2001 From: Daniel Lupu Date: Sat, 26 Nov 2022 21:49:03 +0200 Subject: [PATCH] getObject -> Object::objectAt --- libyul/Object.cpp | 26 ++++++++++++++++++++++++++ libyul/Object.h | 8 ++++++++ test/tools/yulopti.cpp | 36 +----------------------------------- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/libyul/Object.cpp b/libyul/Object.cpp index 83ee66a3b..9263e4696 100644 --- a/libyul/Object.cpp +++ b/libyul/Object.cpp @@ -33,6 +33,7 @@ #include #include +#include #include using namespace solidity; @@ -173,3 +174,28 @@ std::vector Object::pathToSubObject(YulString _qualifiedName) const return path; } + +shared_ptr Object::objectAt(shared_ptr const& _object, string const& _qualifiedPath) +{ + if (_qualifiedPath.empty() || _qualifiedPath == _object->name.str()) + return _object; + + if (!boost::algorithm::starts_with(_qualifiedPath, _object->name.str() + ".")) + return nullptr; + + string const subObjectPath = _qualifiedPath.substr(_object->name.str().length() + 1); + string const subObjectName = subObjectPath.substr(0, subObjectPath.find_first_of('.')); + + auto subObjectIt = ranges::find_if( + _object->subObjects, + [&subObjectName](auto const& _subObject) { return _subObject->name.str() == subObjectName; } + ); + + if (subObjectIt == _object->subObjects.end()) + return nullptr; + + auto subObject = dynamic_pointer_cast(*subObjectIt); + yulAssert(subObject, "Assembly object <" + subObject->name.str() + "> does not contain code."); + + return objectAt(subObject, subObjectPath); +} diff --git a/libyul/Object.h b/libyul/Object.h index 5f88d3bc7..2bbcf1bd7 100644 --- a/libyul/Object.h +++ b/libyul/Object.h @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -128,6 +129,13 @@ public: /// @returns the name of the special metadata data object. static std::string metadataName() { return ".metadata"; } + + /// Recursively searches for an Object at @param _qualifiedPath within @param _object. + /// @returns a shared_ptr to the Object or a nullptr if it was not found. + static std::shared_ptr objectAt( + std::shared_ptr const& _object, + std::string const& _qualifiedPath + ); }; } diff --git a/test/tools/yulopti.cpp b/test/tools/yulopti.cpp index 34d9696b3..55b8b1ca9 100644 --- a/test/tools/yulopti.cpp +++ b/test/tools/yulopti.cpp @@ -51,7 +51,6 @@ #include #include -#include #include #include #include @@ -88,39 +87,6 @@ public: }.printErrorInformation(_errors); } - /// Recursively searches for an object at @param _qualifiedPath within @param _object. - /// @returns the object at @param qualifiedPath or a nullptr if it was not found. - shared_ptr getObject(shared_ptr const& _object, string const& _qualifiedPath) - { - if (_qualifiedPath.empty() || _qualifiedPath == _object->name.str()) - return _object; - - if (!boost::algorithm::starts_with(_qualifiedPath, _object->name.str() + ".")) { - return nullptr; - } - - string const subObjectPath = _qualifiedPath.substr(_object->name.str().length() + 1); - string const subObjectName = subObjectPath.substr(0, subObjectPath.find_first_of('.')); - - auto subObjectIt = ranges::find_if( - _object->subObjects, - [&subObjectName](auto const& _subObject) { return _subObject->name.str() == subObjectName; } - ); - - if (subObjectIt == _object->subObjects.end()) { - return nullptr; - } - - auto subObject = dynamic_pointer_cast(*subObjectIt); - - yulAssert( - subObject != nullptr, - "Assembly object does not contain code." - ); - - return getObject(subObject, subObjectPath); - } - void parse(string const& _input, string const& _objectPath) { ErrorList errors; @@ -145,7 +111,7 @@ public: throw runtime_error("Could not parse source."); } - m_object = getObject(object, _objectPath); + m_object = Object::objectAt(object, _objectPath); if (m_object == nullptr) { throw runtime_error("Assembly object not found.");