Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimír Štill <[email protected]>
  • Loading branch information
vlstill committed Oct 3, 2024
1 parent 04068f4 commit 2e4ce5e
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions ir/ir-traversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace P4::IR::Traversal {
/// @brief A selector used at the end of selector chain to assign to the current sub-object.
/// e.g. `modify(obj, &IR::AssignmentStatement::left, Assign(var))` will set the LHS of assignment.
/// @tparam T the parameter is usually derived by the C++ compiler.
template<typename T>
template <typename T>
struct Assign {
explicit Assign(const T &value) : value(value) {}
T value;
Expand All @@ -53,65 +53,56 @@ namespace Detail {
/// @brief Internal, don't use directly.
/// The class exists so that the functions can be mutually recursive.
struct Traverse {

template<typename Obj, typename T>
static const Obj *modify(const Obj *, Assign<const T *> asgn)
{
template <typename Obj, typename T>
static const Obj *modify(const Obj *, Assign<const T *> asgn) {
return asgn.value;
}

template<typename Obj, typename T>
static const Obj *modify(Obj *, Assign<const T *> asgn)
{
template <typename Obj, typename T>
static const Obj *modify(Obj *, Assign<const T *> asgn) {
return asgn.value;
}

template<typename Obj>
static Obj *modify(Obj *obj, Assign<Obj> &&asgn)
{
template <typename Obj>
static Obj *modify(Obj *obj, Assign<Obj> &&asgn) {
*obj = std::move(asgn.value);
return obj;
}

template<typename Obj>
static Obj *modify(Obj *obj, const Assign<Obj> &asgn)
{
template <typename Obj>
static Obj *modify(Obj *obj, const Assign<Obj> &asgn) {
*obj = asgn.value;
return obj;
}

template<typename Obj, typename Fn>
static decltype(auto) modify(Obj *obj, Fn fn)
{
template <typename Obj, typename Fn>
static decltype(auto) modify(Obj *obj, Fn fn) {
return fn(obj);
}

template<typename Obj, typename... Selectors>
static Obj *modify(Obj *obj, Index idx, Selectors &&... selectors)
{
template <typename Obj, typename... Selectors>
static Obj *modify(Obj *obj, Index idx, Selectors &&...selectors) {
BUG_CHECK(obj->size() > idx.value, "Index %1% out of bounds of %2%", idx.value, obj);
modifyRef((*obj)[idx.value], std::forward<Selectors>(selectors)...);
return obj;
}

template<typename Obj, typename To, typename... Selectors>
static To *modify(Obj *obj, RTTI::Detail::ToType<To> cast, Selectors &&... selectors)
{
template <typename Obj, typename To, typename... Selectors>
static To *modify(Obj *obj, RTTI::Detail::ToType<To> cast, Selectors &&...selectors) {
auto *casted = cast(obj);
BUG_CHECK(casted, "Cast of %1% failed", obj);
return modify(casted, std::forward<Selectors>(selectors)...);
}

template<typename Obj, typename T, typename Sub, typename... Selectors>
static Obj *modify(Obj *obj, Sub T::* member, Selectors &&... selectors)
{
template <typename Obj, typename T, typename Sub, typename... Selectors>
static Obj *modify(Obj *obj, Sub T::*member, Selectors &&...selectors) {
static_assert(!std::is_const_v<Obj>, "Cannot modify constant object");
modifyRef(obj->*member, std::forward<Selectors>(selectors)...);
return obj;
}

template<typename T, typename... Selectors>
static void modifyRef(T &ref, Selectors &&... selectors) {
template <typename T, typename... Selectors>
static void modifyRef(T &ref, Selectors &&...selectors) {
if constexpr (std::is_pointer_v<T>) {
ref = modify(ref->clone(), std::forward<Selectors>(selectors)...);
} else {
Expand All @@ -123,7 +114,7 @@ struct Traverse {
}
};

} // namespace Details
} // namespace Detail

/// @brief Given an object @p obj and a series of selector (ending with a modifier), modify the @p
/// obj's sub object at the selecte path. %This is useful for deeper modification of objects that
Expand Down Expand Up @@ -169,7 +160,7 @@ struct Traverse {
/// return new IR::Cast(expr->srcInfo, idxType, expr);
/// });
/// @endcode
///
///
/// Similarly, you can set the type of the first argument in the type of the extern like this:
/// @code
/// modify(call, &IR::MethodCallStatement::methodCall, &IR::MethodCallExpression::method,
Expand All @@ -178,17 +169,16 @@ struct Traverse {
/// @endcode
///
/// Any time a cast fails or an index is out of range the modification triggers a `BUG`.
template<typename Obj, typename... Selectors>
Obj *modify(Obj *obj, Selectors &&... selectors) {
template <typename Obj, typename... Selectors>
Obj *modify(Obj *obj, Selectors &&...selectors) {
return Detail::Traverse::modify(obj, std::forward<Selectors>(selectors)...);
}

/// @brief Similar to modify, but accepts constant argument which is cloned. Therefore, the result
/// is a different object then @p obj. @sa `Traversal::modify`.
template<typename Obj, typename... Selectors>
Obj *apply(const Obj *obj, Selectors &&... selectors) {
template <typename Obj, typename... Selectors>
Obj *apply(const Obj *obj, Selectors &&...selectors) {
return Detail::Traverse::modify(obj->clone(), std::forward<Selectors>(selectors)...);
}


} // namespace P4::IR::Traversal

0 comments on commit 2e4ce5e

Please sign in to comment.