getObject -> Object::objectAt

This commit is contained in:
Daniel Lupu
2023-10-02 14:35:36 +02:00
committed by r0qs
parent 4095ea50c2
commit 9012ade27f
3 changed files with 35 additions and 35 deletions
+26
View File
@@ -33,6 +33,7 @@
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <range/v3/algorithm/find_if.hpp>
#include <range/v3/view/transform.hpp>
using namespace solidity;
@@ -173,3 +174,28 @@ std::vector<size_t> Object::pathToSubObject(YulString _qualifiedName) const
return path;
}
shared_ptr<Object> Object::objectAt(shared_ptr<Object> 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<Object>(*subObjectIt);
yulAssert(subObject, "Assembly object <" + subObject->name.str() + "> does not contain code.");
return objectAt(subObject, subObjectPath);
}
+8
View File
@@ -31,6 +31,7 @@
#include <memory>
#include <set>
#include <string>
#include <limits>
#include <json/json.h>
@@ -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<Object> objectAt(
std::shared_ptr<Object> const& _object,
std::string const& _qualifiedPath
);
};
}