blob: 362abeecd07ad54beae9941cb23e31c169a6d861 [file] [log] [blame]
Gabor Greifb3c90d92010-07-20 19:35:55 +00001//===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
Gabor Greifee57dae2010-07-20 16:32:20 +00002//
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
Gabor Greifee57dae2010-07-20 16:32:20 +000010#include "llvm/Support/Casting.h"
Gabor Greifb3c90d92010-07-20 19:35:55 +000011#include "llvm/Support/Debug.h"
12#include "llvm/Support/raw_ostream.h"
Rafael Espindola60f18ad2013-07-18 02:42:40 +000013#include "llvm/IR/User.h"
Gabor Greifee57dae2010-07-20 16:32:20 +000014#include "gtest/gtest.h"
15#include <cstdlib>
16
Gabor Greife8950972010-07-20 17:06:28 +000017namespace llvm {
Rafael Espindola60f18ad2013-07-18 02:42:40 +000018// Used to test illegal cast. If a cast doesn't match any of the "real" ones,
19// it will match this one.
20struct IllegalCast;
21template <typename T> IllegalCast *cast(...) { return 0; }
Gabor Greife8950972010-07-20 17:06:28 +000022
23// set up two example classes
24// with conversion facility
25//
26struct bar {
27 bar() {}
Gabor Greifd1594672010-07-22 15:24:48 +000028 struct foo *baz();
29 struct foo *caz();
Gabor Greiff06eb372010-07-22 15:37:20 +000030 struct foo *daz();
31 struct foo *naz();
Gabor Greife8950972010-07-20 17:06:28 +000032private:
33 bar(const bar &);
34};
35struct foo {
36 void ext() const;
37 /* static bool classof(const bar *X) {
38 cerr << "Classof: " << X << "\n";
39 return true;
40 }*/
41};
42
Gabor Greiff06eb372010-07-22 15:37:20 +000043template <> struct isa_impl<foo, bar> {
Gabor Greife8950972010-07-20 17:06:28 +000044 static inline bool doit(const bar &Val) {
45 dbgs() << "Classof: " << &Val << "\n";
46 return true;
47 }
48};
49
Gabor Greifd1594672010-07-22 15:24:48 +000050foo *bar::baz() {
Gabor Greife8950972010-07-20 17:06:28 +000051 return cast<foo>(this);
Gabor Greifd1594672010-07-22 15:24:48 +000052}
53
54foo *bar::caz() {
55 return cast_or_null<foo>(this);
56}
57
Gabor Greiff06eb372010-07-22 15:37:20 +000058foo *bar::daz() {
Gabor Greifd1594672010-07-22 15:24:48 +000059 return dyn_cast<foo>(this);
Gabor Greiff06eb372010-07-22 15:37:20 +000060}
61
62foo *bar::naz() {
63 return dyn_cast_or_null<foo>(this);
64}
Gabor Greife8950972010-07-20 17:06:28 +000065
66
67bar *fub();
Rafael Espindola60f18ad2013-07-18 02:42:40 +000068
69template <> struct simplify_type<foo> {
70 typedef int SimpleType;
71 static SimpleType getSimplifiedValue(foo &Val) { return 0; }
72};
73
Gabor Greife8950972010-07-20 17:06:28 +000074} // End llvm namespace
75
Gabor Greifee57dae2010-07-20 16:32:20 +000076using namespace llvm;
77
Rafael Espindola60f18ad2013-07-18 02:42:40 +000078
79// Test the peculiar behavior of Use in simplify_type.
80int Check1[is_same<simplify_type<Use>::SimpleType, Value *>::value ? 1 : -1];
81int Check2[is_same<simplify_type<Use *>::SimpleType, Value *>::value ? 1 : -1];
82
83// Test that a regular class behaves as expected.
84int Check3[is_same<simplify_type<foo>::SimpleType, int>::value ? 1 : -1];
85int Check4[is_same<simplify_type<foo *>::SimpleType, foo *>::value ? 1 : -1];
86
Gabor Greifee57dae2010-07-20 16:32:20 +000087namespace {
88
Gabor Greif08993c02010-07-20 16:51:18 +000089const foo *null_foo = NULL;
90
NAKAMURA Takumi2a535772012-01-22 12:14:35 +000091bar B;
Gabor Greifee57dae2010-07-20 16:32:20 +000092extern bar &B1;
NAKAMURA Takumi2a535772012-01-22 12:14:35 +000093bar &B1 = B;
Gabor Greifee57dae2010-07-20 16:32:20 +000094extern const bar *B2;
Gabor Greif08993c02010-07-20 16:51:18 +000095// test various configurations of const
96const bar &B3 = B1;
97const bar *const B4 = B2;
Gabor Greifee57dae2010-07-20 16:32:20 +000098
Gabor Greifaf8e2ef2010-07-20 16:38:12 +000099TEST(CastingTest, isa) {
Gabor Greifee57dae2010-07-20 16:32:20 +0000100 EXPECT_TRUE(isa<foo>(B1));
Gabor Greifaf8e2ef2010-07-20 16:38:12 +0000101 EXPECT_TRUE(isa<foo>(B2));
102 EXPECT_TRUE(isa<foo>(B3));
103 EXPECT_TRUE(isa<foo>(B4));
Gabor Greifee57dae2010-07-20 16:32:20 +0000104}
105
Gabor Greif08993c02010-07-20 16:51:18 +0000106TEST(CastingTest, cast) {
107 foo &F1 = cast<foo>(B1);
108 EXPECT_NE(&F1, null_foo);
109 const foo *F3 = cast<foo>(B2);
110 EXPECT_NE(F3, null_foo);
111 const foo *F4 = cast<foo>(B2);
112 EXPECT_NE(F4, null_foo);
Gabor Greife8950972010-07-20 17:06:28 +0000113 const foo &F5 = cast<foo>(B3);
114 EXPECT_NE(&F5, null_foo);
115 const foo *F6 = cast<foo>(B4);
116 EXPECT_NE(F6, null_foo);
Richard Smith79b59a22012-08-21 20:39:25 +0000117 // Can't pass null pointer to cast<>.
118 // foo *F7 = cast<foo>(fub());
119 // EXPECT_EQ(F7, null_foo);
Gabor Greifd1594672010-07-22 15:24:48 +0000120 foo *F8 = B1.baz();
121 EXPECT_NE(F8, null_foo);
Gabor Greif08993c02010-07-20 16:51:18 +0000122}
123
124TEST(CastingTest, cast_or_null) {
125 const foo *F11 = cast_or_null<foo>(B2);
126 EXPECT_NE(F11, null_foo);
127 const foo *F12 = cast_or_null<foo>(B2);
128 EXPECT_NE(F12, null_foo);
129 const foo *F13 = cast_or_null<foo>(B4);
130 EXPECT_NE(F13, null_foo);
131 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
132 EXPECT_EQ(F14, null_foo);
Gabor Greifd1594672010-07-22 15:24:48 +0000133 foo *F15 = B1.caz();
134 EXPECT_NE(F15, null_foo);
135}
136
137TEST(CastingTest, dyn_cast) {
Gabor Greif46a35012010-07-22 15:28:30 +0000138 const foo *F1 = dyn_cast<foo>(B2);
139 EXPECT_NE(F1, null_foo);
140 const foo *F2 = dyn_cast<foo>(B2);
141 EXPECT_NE(F2, null_foo);
142 const foo *F3 = dyn_cast<foo>(B4);
Gabor Greifd1594672010-07-22 15:24:48 +0000143 EXPECT_NE(F3, null_foo);
Richard Smith79b59a22012-08-21 20:39:25 +0000144 // Can't pass null pointer to dyn_cast<>.
145 // foo *F4 = dyn_cast<foo>(fub());
Gabor Greiff06eb372010-07-22 15:37:20 +0000146 // EXPECT_EQ(F4, null_foo);
147 foo *F5 = B1.daz();
148 EXPECT_NE(F5, null_foo);
149}
150
151TEST(CastingTest, dyn_cast_or_null) {
152 const foo *F1 = dyn_cast_or_null<foo>(B2);
153 EXPECT_NE(F1, null_foo);
154 const foo *F2 = dyn_cast_or_null<foo>(B2);
155 EXPECT_NE(F2, null_foo);
156 const foo *F3 = dyn_cast_or_null<foo>(B4);
157 EXPECT_NE(F3, null_foo);
158 foo *F4 = dyn_cast_or_null<foo>(fub());
Gabor Greif46a35012010-07-22 15:28:30 +0000159 EXPECT_EQ(F4, null_foo);
Gabor Greiff06eb372010-07-22 15:37:20 +0000160 foo *F5 = B1.naz();
161 EXPECT_NE(F5, null_foo);
Gabor Greif08993c02010-07-20 16:51:18 +0000162}
163
Gabor Greife8950972010-07-20 17:06:28 +0000164// These lines are errors...
165//foo *F20 = cast<foo>(B2); // Yields const foo*
166//foo &F21 = cast<foo>(B3); // Yields const foo&
167//foo *F22 = cast<foo>(B4); // Yields const foo*
168//foo &F23 = cast_or_null<foo>(B1);
169//const foo &F24 = cast_or_null<foo>(B3);
170
Gabor Greifee57dae2010-07-20 16:32:20 +0000171const bar *B2 = &B;
172} // anonymous namespace
Gabor Greife8950972010-07-20 17:06:28 +0000173
174bar *llvm::fub() { return 0; }
Sean Silva8b8fa7b2012-10-11 23:30:40 +0000175
176namespace {
177namespace inferred_upcasting {
178// This test case verifies correct behavior of inferred upcasts when the
179// types are statically known to be OK to upcast. This is the case when,
180// for example, Derived inherits from Base, and we do `isa<Base>(Derived)`.
181
182// Note: This test will actually fail to compile without inferred
183// upcasting.
184
185class Base {
186public:
187 // No classof. We are testing that the upcast is inferred.
188 Base() {}
189};
190
191class Derived : public Base {
192public:
193 Derived() {}
194};
195
196// Even with no explicit classof() in Base, we should still be able to cast
197// Derived to its base class.
198TEST(CastingTest, UpcastIsInferred) {
199 Derived D;
200 EXPECT_TRUE(isa<Base>(D));
201 Base *BP = dyn_cast<Base>(&D);
202 EXPECT_TRUE(BP != NULL);
203}
204
205
206// This test verifies that the inferred upcast takes precedence over an
207// explicitly written one. This is important because it verifies that the
208// dynamic check gets optimized away.
209class UseInferredUpcast {
210public:
211 int Dummy;
212 static bool classof(const UseInferredUpcast *) {
213 return false;
214 }
215};
216
217TEST(CastingTest, InferredUpcastTakesPrecedence) {
218 UseInferredUpcast UIU;
219 // Since the explicit classof() returns false, this will fail if the
220 // explicit one is used.
221 EXPECT_TRUE(isa<UseInferredUpcast>(&UIU));
222}
223
224} // end namespace inferred_upcasting
225} // end anonymous namespace
Rafael Espindola60f18ad2013-07-18 02:42:40 +0000226// Test that we reject casts of temporaries (and so the illegal cast gets used).
227namespace TemporaryCast {
228struct pod {};
229IllegalCast *testIllegalCast() { return cast<foo>(pod()); }
230}