From 3ef77fcd5545f24773797ecfa0dfa24802c239e6 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 15 Oct 2001 13:41:37 +0000 Subject: [PATCH] Add cast_or_null & dyn_cast_or_null git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@824 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Value.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/include/llvm/Value.h b/include/llvm/Value.h index aece4ce2439..0519bb736e8 100644 --- a/include/llvm/Value.h +++ b/include/llvm/Value.h @@ -204,8 +204,17 @@ inline bool isa(Y Val) { // template inline X *cast(Y Val) { + assert(isa(Val) && "cast() argument of uncompatible type!"); + return (X*)(real_type::Type)Val; +} + +// cast_or_null - Functionally identical to cast, except that a null value is +// accepted. +// +template +inline X *cast_or_null(Y Val) { assert((Val == 0 || isa(Val)) && - "cast() argument of uncompatible type!"); + "cast_or_null() argument of uncompatible type!"); return (X*)(real_type::Type)Val; } @@ -223,6 +232,16 @@ inline X *dyn_cast(Y Val) { return isa(Val) ? cast(Val) : 0; } +// dyn_cast_or_null - Functionally identical to dyn_cast, except that a null +// value is accepted. +// +template +inline X *dyn_cast_or_null(Y Val) { + assert((Val == 0 || isa(Val)) && + "cast_or_null() argument of uncompatible type!"); + return (Val && isa(Val)) ? cast(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...