From 60f18ad8a5fa7d5b008a1fecf9a79b68883ca192 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 18 Jul 2013 02:42:40 +0000 Subject: Fix a regression I introduced back in r178147. We don't want cast and dyn_cast to work on temporaries. They don't extend lifetime like a direct bind to a reference would, so they can introduce hard to find bugs. I added tests to make sure we don't regress this. Thanks to Eli Friedman for noticing this and for his suggestions on how to test it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186559 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/Casting.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'include/llvm') diff --git a/include/llvm/Support/Casting.h b/include/llvm/Support/Casting.h index 0d2d6c92fd..d70acbf24c 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; } -- cgit v1.2.3