Index: include/llvm/Support/Casting.h =================================================================== --- include/llvm/Support/Casting.h +++ include/llvm/Support/Casting.h @@ -18,6 +18,7 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/type_traits.h" #include +#include namespace llvm { @@ -47,6 +48,30 @@ } }; +template struct simplify_type> { + typedef T* SimpleType; + static SimpleType getSimplifiedValue(std::unique_ptr &Val) { return Val.get(); } +}; + +template struct simplify_type> { + typedef /*const*/ T* SimpleType; + static SimpleType getSimplifiedValue(const std::unique_ptr &Val) { + return Val.get(); + } +}; + +template struct simplify_type> { + typedef T* SimpleType; + static SimpleType getSimplifiedValue(std::shared_ptr &Val) { return Val.get(); } +}; + +template struct simplify_type> { + typedef /*const*/ T* SimpleType; + static SimpleType getSimplifiedValue(const std::shared_ptr &Val) { + return Val.get(); + } +}; + // The core of the implementation of isa is here; To and From should be // the names of classes. This template can be specialized to customize the // implementation of isa<> without rewriting it from scratch. Index: unittests/Support/Casting.cpp =================================================================== --- unittests/Support/Casting.cpp +++ unittests/Support/Casting.cpp @@ -165,6 +165,26 @@ EXPECT_NE(F5, null_foo); } +// We should be able to treat unique_ptr just like T* for the purposes of +// isa, dyn_cast, cast, etc. +TEST(CastingTest, unique_ptr) { + std::unique_ptr a(new foo()); + EXPECT_TRUE(isa(a)); + + std::unique_ptr b(new bar()); + EXPECT_NE(dyn_cast_or_null(b), null_foo); +} + +// We should be able to treat shared_ptr just like T* for the purposes of +// isa, dyn_cast, cast, etc. +TEST(CastingTest, shared_ptr) { + auto a = std::make_shared(); + EXPECT_TRUE(isa(a)); + + auto b = std::make_shared(); + EXPECT_NE(dyn_cast_or_null(b), null_foo); +} + // These lines are errors... //foo *F20 = cast(B2); // Yields const foo* //foo &F21 = cast(B3); // Yields const foo&