Add cast_or_null & dyn_cast_or_null
authorChris Lattner <sabre@nondot.org>
Mon, 15 Oct 2001 13:41:37 +0000 (13:41 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 15 Oct 2001 13:41:37 +0000 (13:41 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@824 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Value.h

index aece4ce2439682c5e052d7ba3358a7962d619694..0519bb736e8af5f8721b3d4bf427a1b423d59249 100644 (file)
@@ -204,8 +204,17 @@ inline bool isa(Y Val) {
 //
 template <class X, class Y>
 inline X *cast(Y Val) {
+  assert(isa<X>(Val) && "cast<Ty>() argument of uncompatible type!");
+  return (X*)(real_type<Y>::Type)Val;
+}
+
+// cast_or_null<X> - Functionally identical to cast, except that a null value is
+// accepted.
+//
+template <class X, class Y>
+inline X *cast_or_null(Y Val) {
   assert((Val == 0 || isa<X>(Val)) &&
-         "cast<Ty>() argument of uncompatible type!");
+         "cast_or_null<Ty>() argument of uncompatible type!");
   return (X*)(real_type<Y>::Type)Val;
 }
 
@@ -223,6 +232,16 @@ inline X *dyn_cast(Y Val) {
   return isa<X>(Val) ? cast<X>(Val) : 0;
 }
 
+// dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
+// value is accepted.
+//
+template <class X, class Y>
+inline X *dyn_cast_or_null(Y Val) {
+  assert((Val == 0 || isa<X>(Val)) &&
+         "cast_or_null<Ty>() argument of uncompatible type!");
+  return (Val && isa<X>(Val)) ? cast<X>(Val) : 0;
+}
+
 
 // isa - Provide some specializations of isa so that we have to include the
 // subtype header files to test to see if the value is a subclass...