diff --git a/include/llvm/Support/Casting.h b/include/llvm/Support/Casting.h index 0d2d6c92fdb..d70acbf24c4 100644 --- a/include/llvm/Support/Casting.h +++ b/include/llvm/Support/Casting.h @@ -206,7 +206,10 @@ template struct cast_convert_val { } }; - +template struct is_simple_type { + static const bool value = + is_same::SimpleType>::value; +}; // cast - Return the argument parameter cast to the specified type. This // casting operator asserts that the type is correct, so it does not return null @@ -216,10 +219,12 @@ template struct cast_convert_val { // cast(myVal)->getParent() // template -inline typename cast_retty::ret_type cast(const Y &Val) { +inline typename enable_if_c::value, + typename cast_retty::ret_type>::type +cast(const Y &Val) { assert(isa(Val) && "cast() argument of incompatible type!"); - return cast_convert_val::SimpleType>::doit(Val); + return cast_convert_val< + X, const Y, typename simplify_type::SimpleType>::doit(Val); } template @@ -230,10 +235,7 @@ inline typename cast_retty::ret_type cast(Y &Val) { } template -inline typename enable_if< - is_same::SimpleType>, - typename cast_retty::ret_type ->::type cast(Y *Val) { +inline typename cast_retty::ret_type cast(Y *Val) { assert(isa(Val) && "cast() argument of incompatible type!"); return cast_convert_val::SimpleType>::doit(Val); @@ -259,7 +261,9 @@ inline typename cast_retty::ret_type cast_or_null(Y *Val) { // template -inline typename cast_retty::ret_type dyn_cast(const Y &Val) { +inline typename enable_if_c::value, + typename cast_retty::ret_type>::type +dyn_cast(const Y &Val) { return isa(Val) ? cast(Val) : 0; } @@ -269,10 +273,7 @@ inline typename cast_retty::ret_type dyn_cast(Y &Val) { } template -inline typename enable_if< - is_same::SimpleType>, - typename cast_retty::ret_type ->::type dyn_cast(Y *Val) { +inline typename cast_retty::ret_type dyn_cast(Y *Val) { return isa(Val) ? cast(Val) : 0; } diff --git a/unittests/Support/Casting.cpp b/unittests/Support/Casting.cpp index 01583e43e29..362abeecd07 100644 --- a/unittests/Support/Casting.cpp +++ b/unittests/Support/Casting.cpp @@ -10,10 +10,15 @@ #include "llvm/Support/Casting.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/IR/User.h" #include "gtest/gtest.h" #include namespace llvm { +// Used to test illegal cast. If a cast doesn't match any of the "real" ones, +// it will match this one. +struct IllegalCast; +template IllegalCast *cast(...) { return 0; } // set up two example classes // with conversion facility @@ -60,10 +65,25 @@ foo *bar::naz() { bar *fub(); + +template <> struct simplify_type { + typedef int SimpleType; + static SimpleType getSimplifiedValue(foo &Val) { return 0; } +}; + } // End llvm namespace using namespace llvm; + +// Test the peculiar behavior of Use in simplify_type. +int Check1[is_same::SimpleType, Value *>::value ? 1 : -1]; +int Check2[is_same::SimpleType, Value *>::value ? 1 : -1]; + +// Test that a regular class behaves as expected. +int Check3[is_same::SimpleType, int>::value ? 1 : -1]; +int Check4[is_same::SimpleType, foo *>::value ? 1 : -1]; + namespace { const foo *null_foo = NULL; @@ -203,3 +223,8 @@ TEST(CastingTest, InferredUpcastTakesPrecedence) { } // end namespace inferred_upcasting } // end anonymous namespace +// Test that we reject casts of temporaries (and so the illegal cast gets used). +namespace TemporaryCast { +struct pod {}; +IllegalCast *testIllegalCast() { return cast(pod()); } +}