[PATCH] D125590: [LLVM][Casting.h] Add trivial self-cast

Aman LaChapelle via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun May 15 22:22:28 PDT 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG0809f63826d3: [LLVM][Casting.h] Add trivial self-cast (authored by bzcheeseman).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D125590/new/

https://reviews.llvm.org/D125590

Files:
  llvm/include/llvm/Support/Casting.h
  llvm/unittests/Support/Casting.cpp


Index: llvm/unittests/Support/Casting.cpp
===================================================================
--- llvm/unittests/Support/Casting.cpp
+++ llvm/unittests/Support/Casting.cpp
@@ -144,6 +144,7 @@
 // test various configurations of const
 const bar &B3 = B1;
 const bar *const B4 = B2;
+bar *B5 = &B;
 
 TEST(CastingTest, isa) {
   EXPECT_TRUE(isa<foo>(B1));
@@ -175,6 +176,17 @@
   foo *F8 = B1.baz();
   EXPECT_NE(F8, null_foo);
 
+  // Ensure cast-to-self works (with the type in the template verbatim
+  // equivalent to the type in the input).
+  auto B9 = cast<bar *>(B5);
+  static_assert(std::is_same<bar *, decltype(B9)>::value,
+                "Inccorrect return type!");
+  EXPECT_EQ(B9, B5);
+  auto B10 = cast<const bar *>(B2);
+  static_assert(std::is_same<const bar *, decltype(B10)>::value,
+                "Inccorrect return type!");
+  EXPECT_EQ(B10, B2);
+
   std::unique_ptr<const bar> BP(B2);
   auto FP = cast<foo>(std::move(BP));
   static_assert(std::is_same<std::unique_ptr<const foo>, decltype(FP)>::value,
@@ -212,6 +224,17 @@
   // EXPECT_EQ(F4, null_foo);
   foo *F5 = B1.daz();
   EXPECT_NE(F5, null_foo);
+
+  // Ensure cast-to-self works (with the type in the template verbatim
+  // equivalent to the type in the input).
+  auto B9 = dyn_cast<bar *>(B5);
+  static_assert(std::is_same<bar *, decltype(B9)>::value,
+                "Inccorrect return type!");
+  EXPECT_EQ(B9, B5);
+  auto B10 = dyn_cast<const bar *>(B2);
+  static_assert(std::is_same<const bar *, decltype(B10)>::value,
+                "Inccorrect return type!");
+  EXPECT_EQ(B10, B2);
 }
 
 // All these tests forward to dyn_cast_if_present, so they also provde an
Index: llvm/include/llvm/Support/Casting.h
===================================================================
--- llvm/include/llvm/Support/Casting.h
+++ llvm/include/llvm/Support/Casting.h
@@ -192,6 +192,12 @@
       To, From, typename simplify_type<From>::SimpleType>::ret_type;
 };
 
+template <class T> struct cast_retty<T, T> {
+  // Casting T to itself, but have to use a ref if so we don't get a copy if
+  // it's not a pointer.
+  using ret_type = std::conditional_t<std::is_pointer<T>::value, T, T &>;
+};
+
 //===----------------------------------------------------------------------===//
 // cast_convert_val
 //===----------------------------------------------------------------------===//
@@ -276,7 +282,8 @@
 /// always be possible.
 template <typename To, typename From>
 struct CastIsPossible<To, From,
-                      std::enable_if_t<std::is_base_of<To, From>::value>> {
+                      std::enable_if_t<std::is_same<To, From>::value ||
+                                       std::is_base_of<To, From>::value>> {
   static inline bool isPossible(const From &f) { return true; }
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125590.429609.patch
Type: text/x-patch
Size: 2801 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220516/a1968624/attachment.bin>


More information about the llvm-commits mailing list