mirror of
https://github.com/revng/revng
synced 2026-06-21 14:07:57 +00:00
d2967d2a49
This commit removes the inclusion of the `Binary.h` header in Tuple-Tree Generator files, allowing Model-independent Tuple Trees to be generated.
79 lines
1.5 KiB
C++
79 lines
1.5 KiB
C++
#pragma once
|
|
|
|
//
|
|
// This file is distributed under the MIT License. See LICENSE.md for details.
|
|
//
|
|
|
|
#include "revng/TupleTree/Visits.h"
|
|
|
|
//
|
|
// getByPath
|
|
//
|
|
namespace tupletree::detail {
|
|
|
|
template<typename ResultT>
|
|
struct GetByPathVisitor {
|
|
ResultT *Result = nullptr;
|
|
|
|
template<typename T, typename K, typename KeyT>
|
|
void visitContainerElement(KeyT, K &) {
|
|
Result = nullptr;
|
|
}
|
|
|
|
template<typename T, typename KeyT>
|
|
void visitContainerElement(KeyT, ResultT &Element) {
|
|
Result = ∈
|
|
}
|
|
|
|
template<typename, size_t, typename K>
|
|
void visitTupleElement(K &) {
|
|
Result = nullptr;
|
|
}
|
|
|
|
template<typename, size_t>
|
|
void visitTupleElement(ResultT &Element) {
|
|
Result = ∈
|
|
}
|
|
};
|
|
|
|
} // namespace tupletree::detail
|
|
|
|
template<typename ResultT, typename RootT>
|
|
ResultT *getByPath(const TupleTreePath &Path, RootT &M) {
|
|
using namespace tupletree::detail;
|
|
GetByPathVisitor<ResultT> GBPV;
|
|
if (not callByPath(GBPV, Path, M)) {
|
|
return nullptr;
|
|
}
|
|
|
|
return GBPV.Result;
|
|
}
|
|
|
|
//
|
|
// stringAsPath
|
|
//
|
|
template<typename T>
|
|
std::optional<TupleTreePath> stringAsPath(llvm::StringRef Path) {
|
|
if (Path.empty())
|
|
return std::nullopt;
|
|
|
|
auto Result = PathMatcher::create<T>(Path);
|
|
if (Result)
|
|
return Result->path();
|
|
else
|
|
return std::nullopt;
|
|
}
|
|
|
|
//
|
|
// PathMatcher
|
|
//
|
|
template<typename T>
|
|
std::optional<PathMatcher> PathMatcher::create(llvm::StringRef Path) {
|
|
revng_assert(Path.startswith("/"));
|
|
PathMatcher Result;
|
|
if (visitTupleTreeNode<T>(Path.substr(1), Result))
|
|
return Result;
|
|
else
|
|
return {};
|
|
}
|