1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- llvm/Support/Casting.h - Allow flexible, checked, casts -*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// and dyn_cast_or_null<X>() templates.
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_SUPPORT_CASTING_H
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_SUPPORT_CASTING_H
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <cassert>
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                          isa<x> Support Templates
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Define a template that can be specialized by smart pointers to reflect the
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// fact that they are automatically dereferenced, and are not involved with the
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// template selection process...  the default implementation is a noop.
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<typename From> struct simplify_type {
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef       From SimpleType;        // The real type this represents...
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // An accessor to get the real value...
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static SimpleType &getSimplifiedValue(From &Val) { return Val; }
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<typename From> struct simplify_type<const From> {
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef const From SimpleType;
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static SimpleType &getSimplifiedValue(const From &Val) {
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return simplify_type<From>::getSimplifiedValue(static_cast<From&>(Val));
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// The core of the implementation of isa<X> is here; To and From should be
4519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// the names of classes.  This template can be specialized to customize the
4619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// implementation of isa<> without rewriting it from scratch.
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <typename To, typename From>
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct isa_impl {
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit(const From &Val) {
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return To::classof(&Val);
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <typename To, typename From> struct isa_impl_cl {
5519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static inline bool doit(const From &Val) {
5619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return isa_impl<To, From>::doit(Val);
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <typename To, typename From> struct isa_impl_cl<To, const From> {
6119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static inline bool doit(const From &Val) {
6219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return isa_impl<To, From>::doit(Val);
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <typename To, typename From> struct isa_impl_cl<To, From*> {
6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static inline bool doit(const From *Val) {
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return isa_impl<To, From>::doit(*Val);
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <typename To, typename From> struct isa_impl_cl<To, const From*> {
7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static inline bool doit(const From *Val) {
7419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return isa_impl<To, From>::doit(*Val);
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate <typename To, typename From> struct isa_impl_cl<To, const From*const> {
7919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static inline bool doit(const From *Val) {
8019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return isa_impl<To, From>::doit(*Val);
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate<typename To, typename From, typename SimpleFrom>
8519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstruct isa_impl_wrap {
8619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // When From != SimplifiedType, we can simplify the type some more by using
8719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // the simplify_type template.
8819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static bool doit(const From &Val) {
8919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return isa_impl_wrap<To, SimpleFrom,
9019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      typename simplify_type<SimpleFrom>::SimpleType>::doit(
9119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          simplify_type<From>::getSimplifiedValue(Val));
9219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman};
9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
9519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumantemplate<typename To, typename FromTy>
9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstruct isa_impl_wrap<To, FromTy, FromTy> {
9719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // When From == SimpleType, we are as simple as we are going to get.
9819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static bool doit(const FromTy &Val) {
9919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return isa_impl_cl<To,FromTy>::doit(Val);
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// isa<X> - Return true if the parameter to the template is an instance of the
10419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// template type argument.  Used like this:
10519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman//
10619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman//  if (isa<Type>(myVal)) { ... }
10719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman//
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <class X, class Y>
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline bool isa(const Y &Val) {
11019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return isa_impl_wrap<X, Y, typename simplify_type<Y>::SimpleType>::doit(Val);
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                          cast<x> Support Templates
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class From> struct cast_retty;
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Calculate what type the 'cast' function should return, based on a requested
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// type of To and a source type of From.
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class From> struct cast_retty_impl {
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef To& ret_type;         // Normal case, return Ty&
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class From> struct cast_retty_impl<To, const From> {
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef const To &ret_type;   // Normal case, return Ty&
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class From> struct cast_retty_impl<To, From*> {
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef To* ret_type;         // Pointer arg case, return Ty*
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class From> struct cast_retty_impl<To, const From*> {
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class From> struct cast_retty_impl<To, const From*const> {
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class From, class SimpleFrom>
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct cast_retty_wrap {
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // When the simplified type and the from type are not the same, use the type
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // simplifier to reduce the type, then reuse cast_retty_impl to get the
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // resultant type.
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class FromTy>
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct cast_retty_wrap<To, FromTy, FromTy> {
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // When the simplified type is equal to the from type, use it directly.
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class From>
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct cast_retty {
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef typename cast_retty_wrap<To, From,
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                   typename simplify_type<From>::SimpleType>::ret_type ret_type;
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Ensure the non-simple values are converted using the simplify_type template
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// that may be specialized by smart pointers...
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class From, class SimpleFrom> struct cast_convert_val {
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This is not a simple type, use the template to simplify it...
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static typename cast_retty<To, From>::ret_type doit(const From &Val) {
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return cast_convert_val<To, SimpleFrom,
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      typename simplify_type<SimpleFrom>::SimpleType>::doit(
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          simplify_type<From>::getSimplifiedValue(Val));
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This _is_ a simple type, just cast it.
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    typename cast_retty<To, FromTy>::ret_type Res2
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Res2;
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// cast<X> - Return the argument parameter cast to the specified type.  This
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// casting operator asserts that the type is correct, so it does not return null
18719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// on failure.  It does not allow a null argument (use cast_or_null for that).
18819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// It is typically used like this:
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  cast<Instruction>(myVal)->getParent()
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <class X, class Y>
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return cast_convert_val<X, Y,
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          typename simplify_type<Y>::SimpleType>::doit(Val);
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// cast_or_null<X> - Functionally identical to cast, except that a null value is
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// accepted.
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <class X, class Y>
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Val == 0) return 0;
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return cast<X>(Val);
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// dyn_cast<X> - Return the argument parameter cast to the specified type.  This
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// casting operator returns null if the argument is of the wrong type, so it can
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// be used to test for a type as well as cast if successful.  This should be
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// used in the context of an if statement like this:
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <class X, class Y>
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline typename cast_retty<X, Y>::ret_type dyn_cast(const Y &Val) {
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return isa<X>(Val) ? cast<X, Y>(Val) : 0;
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// value is accepted.
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <class X, class Y>
22719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumaninline typename cast_retty<X, Y*>::ret_type dyn_cast_or_null(Y *Val) {
22819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return (Val && isa<X>(Val)) ? cast<X>(Val) : 0;
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End llvm namespace
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
234