Casting.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Support/Casting.h"
  10. #include "llvm/Support/Debug.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include "gtest/gtest.h"
  13. #include <cstdlib>
  14. namespace llvm {
  15. // set up two example classes
  16. // with conversion facility
  17. //
  18. struct bar {
  19. bar() {}
  20. struct foo *baz();
  21. struct foo *caz();
  22. struct foo *daz();
  23. struct foo *naz();
  24. private:
  25. bar(const bar &);
  26. };
  27. struct foo {
  28. void ext() const;
  29. /* static bool classof(const bar *X) {
  30. cerr << "Classof: " << X << "\n";
  31. return true;
  32. }*/
  33. };
  34. template <> struct isa_impl<foo, bar> {
  35. static inline bool doit(const bar &Val) {
  36. dbgs() << "Classof: " << &Val << "\n";
  37. return true;
  38. }
  39. };
  40. foo *bar::baz() {
  41. return cast<foo>(this);
  42. }
  43. foo *bar::caz() {
  44. return cast_or_null<foo>(this);
  45. }
  46. foo *bar::daz() {
  47. return dyn_cast<foo>(this);
  48. }
  49. foo *bar::naz() {
  50. return dyn_cast_or_null<foo>(this);
  51. }
  52. bar *fub();
  53. } // End llvm namespace
  54. using namespace llvm;
  55. namespace {
  56. const foo *null_foo = NULL;
  57. bar B;
  58. extern bar &B1;
  59. bar &B1 = B;
  60. extern const bar *B2;
  61. // test various configurations of const
  62. const bar &B3 = B1;
  63. const bar *const B4 = B2;
  64. TEST(CastingTest, isa) {
  65. EXPECT_TRUE(isa<foo>(B1));
  66. EXPECT_TRUE(isa<foo>(B2));
  67. EXPECT_TRUE(isa<foo>(B3));
  68. EXPECT_TRUE(isa<foo>(B4));
  69. }
  70. TEST(CastingTest, cast) {
  71. foo &F1 = cast<foo>(B1);
  72. EXPECT_NE(&F1, null_foo);
  73. const foo *F3 = cast<foo>(B2);
  74. EXPECT_NE(F3, null_foo);
  75. const foo *F4 = cast<foo>(B2);
  76. EXPECT_NE(F4, null_foo);
  77. const foo &F5 = cast<foo>(B3);
  78. EXPECT_NE(&F5, null_foo);
  79. const foo *F6 = cast<foo>(B4);
  80. EXPECT_NE(F6, null_foo);
  81. // Can't pass null pointer to cast<>.
  82. // foo *F7 = cast<foo>(fub());
  83. // EXPECT_EQ(F7, null_foo);
  84. foo *F8 = B1.baz();
  85. EXPECT_NE(F8, null_foo);
  86. }
  87. TEST(CastingTest, cast_or_null) {
  88. const foo *F11 = cast_or_null<foo>(B2);
  89. EXPECT_NE(F11, null_foo);
  90. const foo *F12 = cast_or_null<foo>(B2);
  91. EXPECT_NE(F12, null_foo);
  92. const foo *F13 = cast_or_null<foo>(B4);
  93. EXPECT_NE(F13, null_foo);
  94. const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
  95. EXPECT_EQ(F14, null_foo);
  96. foo *F15 = B1.caz();
  97. EXPECT_NE(F15, null_foo);
  98. }
  99. TEST(CastingTest, dyn_cast) {
  100. const foo *F1 = dyn_cast<foo>(B2);
  101. EXPECT_NE(F1, null_foo);
  102. const foo *F2 = dyn_cast<foo>(B2);
  103. EXPECT_NE(F2, null_foo);
  104. const foo *F3 = dyn_cast<foo>(B4);
  105. EXPECT_NE(F3, null_foo);
  106. // Can't pass null pointer to dyn_cast<>.
  107. // foo *F4 = dyn_cast<foo>(fub());
  108. // EXPECT_EQ(F4, null_foo);
  109. foo *F5 = B1.daz();
  110. EXPECT_NE(F5, null_foo);
  111. }
  112. TEST(CastingTest, dyn_cast_or_null) {
  113. const foo *F1 = dyn_cast_or_null<foo>(B2);
  114. EXPECT_NE(F1, null_foo);
  115. const foo *F2 = dyn_cast_or_null<foo>(B2);
  116. EXPECT_NE(F2, null_foo);
  117. const foo *F3 = dyn_cast_or_null<foo>(B4);
  118. EXPECT_NE(F3, null_foo);
  119. foo *F4 = dyn_cast_or_null<foo>(fub());
  120. EXPECT_EQ(F4, null_foo);
  121. foo *F5 = B1.naz();
  122. EXPECT_NE(F5, null_foo);
  123. }
  124. // These lines are errors...
  125. //foo *F20 = cast<foo>(B2); // Yields const foo*
  126. //foo &F21 = cast<foo>(B3); // Yields const foo&
  127. //foo *F22 = cast<foo>(B4); // Yields const foo*
  128. //foo &F23 = cast_or_null<foo>(B1);
  129. //const foo &F24 = cast_or_null<foo>(B3);
  130. const bar *B2 = &B;
  131. } // anonymous namespace
  132. bar *llvm::fub() { return 0; }
  133. namespace {
  134. namespace inferred_upcasting {
  135. // This test case verifies correct behavior of inferred upcasts when the
  136. // types are statically known to be OK to upcast. This is the case when,
  137. // for example, Derived inherits from Base, and we do `isa<Base>(Derived)`.
  138. // Note: This test will actually fail to compile without inferred
  139. // upcasting.
  140. class Base {
  141. public:
  142. // No classof. We are testing that the upcast is inferred.
  143. Base() {}
  144. };
  145. class Derived : public Base {
  146. public:
  147. Derived() {}
  148. };
  149. // Even with no explicit classof() in Base, we should still be able to cast
  150. // Derived to its base class.
  151. TEST(CastingTest, UpcastIsInferred) {
  152. Derived D;
  153. EXPECT_TRUE(isa<Base>(D));
  154. Base *BP = dyn_cast<Base>(&D);
  155. EXPECT_TRUE(BP != NULL);
  156. }
  157. // This test verifies that the inferred upcast takes precedence over an
  158. // explicitly written one. This is important because it verifies that the
  159. // dynamic check gets optimized away.
  160. class UseInferredUpcast {
  161. public:
  162. int Dummy;
  163. static bool classof(const UseInferredUpcast *) {
  164. return false;
  165. }
  166. };
  167. TEST(CastingTest, InferredUpcastTakesPrecedence) {
  168. UseInferredUpcast UIU;
  169. // Since the explicit classof() returns false, this will fail if the
  170. // explicit one is used.
  171. EXPECT_TRUE(isa<UseInferredUpcast>(&UIU));
  172. }
  173. } // end namespace inferred_upcasting
  174. } // end anonymous namespace