Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions llvm/include/llvm/Support/Casting.h
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,42 @@ template <typename... Types> struct IsaAndPresentCheckPredicate {
return isa_and_present<Types...>(Val);
}
};

//===----------------------------------------------------------------------===//
// Casting Function Objects
//===----------------------------------------------------------------------===//

/// Usable in generic algorithms like map_range
template <typename U> struct StaticCastFunc {
template <typename T> decltype(auto) operator()(T &&Val) const {
return static_cast<U>(Val);
}
};

template <typename U> struct DynCastFunc {
template <typename T> decltype(auto) operator()(T &&Val) const {
return dyn_cast<U>(Val);
}
};

template <typename U> struct CastFunc {
template <typename T> decltype(auto) operator()(T &&Val) const {
return cast<U>(Val);
}
};

template <typename U> struct CastIfPresentFunc {
template <typename T> decltype(auto) operator()(T &&Val) const {
return cast_if_present<U>(Val);
}
};

template <typename U> struct DynCastIfPresentFunc {
template <typename T> decltype(auto) operator()(T &&Val) const {
return dyn_cast_if_present<U>(Val);
}
};

} // namespace detail

/// Function object wrapper for the `llvm::isa` type check. The function call
Expand All @@ -841,6 +877,20 @@ template <typename... Types>
inline constexpr detail::IsaAndPresentCheckPredicate<Types...>
IsaAndPresentPred{};

/// Function objects corresponding to the Cast types defined above.
template <typename From>
inline constexpr detail::StaticCastFunc<From> StaticCastTo{};

template <typename From> inline constexpr detail::CastFunc<From> CastTo{};

template <typename From>
inline constexpr detail::CastIfPresentFunc<From> CastIfPresentTo{};

template <typename From>
inline constexpr detail::DynCastIfPresentFunc<From> DynCastIfPresentTo{};

template <typename From> inline constexpr detail::DynCastFunc<From> DynCastTo{};

} // end namespace llvm

#endif // LLVM_SUPPORT_CASTING_H
41 changes: 41 additions & 0 deletions llvm/unittests/Support/Casting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,47 @@ TEST(CastingTest, assertion_check_unique_ptr) {
<< "Invalid cast of const ref did not cause an abort()";
}

TEST(Casting, StaticCastPredicate) {
uint32_t Value = 1;

static_assert(
(std::is_same_v<decltype(StaticCastTo<uint64_t>(Value)), uint64_t>));
}

TEST(Casting, LLVMRTTIPredicates) {
struct Base {
enum Kind { BK_Base, BK_Derived };
const Kind K;
Base(Kind K = BK_Base) : K(K) {}
Kind getKind() const { return K; }
virtual ~Base() = default;
};

struct Derived : Base {
Derived() : Base(BK_Derived) {}
static bool classof(const Base *B) { return B->getKind() == BK_Derived; }
bool Field = false;
};

Base B;
Derived D;
Base *BD = &D;
Base *Null = nullptr;

// pointers
EXPECT_EQ(DynCastTo<Derived>(BD), &D);
EXPECT_EQ(CastTo<Derived>(BD), &D);
EXPECT_EQ(DynCastTo<Derived>(&B), nullptr);
EXPECT_EQ(CastIfPresentTo<Derived>(BD), &D);
EXPECT_EQ(CastIfPresentTo<Derived>(Null), nullptr);
EXPECT_EQ(DynCastIfPresentTo<Derived>(BD), &D);
EXPECT_EQ(DynCastIfPresentTo<Derived>(Null), nullptr);

Base &R = D;
CastTo<Derived>(R).Field = true;
EXPECT_TRUE(D.Field);
}

} // end namespace assertion_checks
#endif
} // end namespace