LLVM API Documentation

PointerUnion.h

Go to the documentation of this file.
00001 //===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the PointerUnion class, which is a discriminated union of
00011 // pointer types.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_ADT_POINTERUNION_H
00016 #define LLVM_ADT_POINTERUNION_H
00017 
00018 #include "llvm/ADT/PointerIntPair.h"
00019 
00020 namespace llvm {
00021 
00022   /// getPointerUnionTypeNum - If the argument has type PT1* or PT2* return
00023   /// false or true respectively.
00024   template <typename PT1, typename PT2>
00025   static inline int getPointerUnionTypeNum(PT1 *P) { return 0; }
00026   template <typename PT1, typename PT2>
00027   static inline int getPointerUnionTypeNum(PT2 *P) { return 1; }
00028   template <typename PT1, typename PT2>
00029   static inline int getPointerUnionTypeNum(...) { return -1; }
00030   
00031   
00032   /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion
00033   /// for the two template arguments.
00034   template <typename PT1, typename PT2>
00035   class PointerUnionUIntTraits {
00036   public:
00037     static inline void *getAsVoidPointer(void *P) { return P; }
00038     static inline void *getFromVoidPointer(void *P) { return P; }
00039     enum {
00040       PT1BitsAv = PointerLikeTypeTraits<PT1>::NumLowBitsAvailable,
00041       PT2BitsAv = PointerLikeTypeTraits<PT2>::NumLowBitsAvailable,
00042       NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv
00043     };
00044   };
00045   
00046   /// PointerUnion - This implements a discriminated union of two pointer types,
00047   /// and keeps the discriminator bit-mangled into the low bits of the pointer.
00048   /// This allows the implementation to be extremely efficient in space, but
00049   /// permits a very natural and type-safe API.
00050   ///
00051   /// Common use patterns would be something like this:
00052   ///    PointerUnion<int*, float*> P;
00053   ///    P = (int*)0;
00054   ///    printf("%d %d", P.is<int*>(), P.is<float*>());  // prints "1 0"
00055   ///    X = P.get<int*>();     // ok.
00056   ///    Y = P.get<float*>();   // runtime assertion failure.
00057   ///    Z = P.get<double*>();  // runtime assertion failure (regardless of tag)
00058   ///    P = (float*)0;
00059   ///    Y = P.get<float*>();   // ok.
00060   ///    X = P.get<int*>();     // runtime assertion failure.
00061   template <typename PT1, typename PT2>
00062   class PointerUnion {
00063   public:
00064     typedef PointerIntPair<void*, 1, bool, 
00065                            PointerUnionUIntTraits<PT1,PT2> > ValTy;
00066   private:
00067     ValTy Val;
00068   public:
00069     PointerUnion() {}
00070     
00071     PointerUnion(PT1 V) {
00072       Val.setPointer(
00073          const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(V)));
00074       Val.setInt(0);
00075     }
00076     PointerUnion(PT2 V) {
00077       Val.setPointer(
00078          const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(V)));
00079       Val.setInt(1);
00080     }
00081     
00082     /// isNull - Return true if the pointer held in the union is null,
00083     /// regardless of which type it is.
00084     bool isNull() const { return Val.getPointer() == 0; }
00085     operator bool() const { return !isNull(); }
00086 
00087     /// is<T>() return true if the Union currently holds the type matching T.
00088     template<typename T>
00089     int is() const {
00090       int TyNo = ::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0);
00091       assert(TyNo != -1 && "Type query could never succeed on PointerUnion!");
00092       return static_cast<int>(Val.getInt()) == TyNo;
00093     }
00094     
00095     /// get<T>() - Return the value of the specified pointer type. If the
00096     /// specified pointer type is incorrect, assert.
00097     template<typename T>
00098     T get() const {
00099       assert(is<T>() && "Invalid accessor called");
00100       return PointerLikeTypeTraits<T>::getFromVoidPointer(Val.getPointer());
00101     }
00102     
00103     /// dyn_cast<T>() - If the current value is of the specified pointer type,
00104     /// return it, otherwise return null.
00105     template<typename T>
00106     T dyn_cast() const {
00107       if (is<T>()) return get<T>();
00108       return T();
00109     }
00110     
00111     /// Assignment operators - Allow assigning into this union from either
00112     /// pointer type, setting the discriminator to remember what it came from.
00113     const PointerUnion &operator=(const PT1 &RHS) {
00114       Val.setPointer(
00115          const_cast<void *>(PointerLikeTypeTraits<PT1>::getAsVoidPointer(RHS)));
00116       Val.setInt(0);
00117       return *this;
00118     }
00119     const PointerUnion &operator=(const PT2 &RHS) {
00120       Val.setPointer(
00121         const_cast<void *>(PointerLikeTypeTraits<PT2>::getAsVoidPointer(RHS)));
00122       Val.setInt(1);
00123       return *this;
00124     }
00125     
00126     void *getOpaqueValue() const { return Val.getOpaqueValue(); }
00127     static PointerUnion getFromOpaqueValue(void *VP) {
00128       PointerUnion V;
00129       V.Val = ValTy::getFromOpaqueValue(VP);
00130       return V;
00131     }
00132   };
00133   
00134   // Teach SmallPtrSet that PointerUnion is "basically a pointer", that has
00135   // # low bits available = min(PT1bits,PT2bits)-1.
00136   template<typename PT1, typename PT2>
00137   class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > {
00138   public:
00139     static inline void *
00140     getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
00141       return P.getOpaqueValue();
00142     }
00143     static inline PointerUnion<PT1, PT2>
00144     getFromVoidPointer(void *P) {
00145       return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
00146     }
00147     
00148     // The number of bits available are the min of the two pointer types.
00149     enum {
00150       NumLowBitsAvailable = 
00151         PointerLikeTypeTraits<typename PointerUnion<PT1,PT2>::ValTy>
00152           ::NumLowBitsAvailable
00153     };
00154   };
00155   
00156   
00157   /// PointerUnion3 - This is a pointer union of three pointer types.  See
00158   /// documentation for PointerUnion for usage.
00159   template <typename PT1, typename PT2, typename PT3>
00160   class PointerUnion3 {
00161   public:
00162     typedef PointerUnion<PT1, PT2> InnerUnion;
00163     typedef PointerUnion<InnerUnion, PT3> ValTy;
00164   private:
00165     ValTy Val;
00166   public:
00167     PointerUnion3() {}
00168     
00169     PointerUnion3(PT1 V) {
00170       Val = InnerUnion(V);
00171     }
00172     PointerUnion3(PT2 V) {
00173       Val = InnerUnion(V);
00174     }
00175     PointerUnion3(PT3 V) {
00176       Val = V;
00177     }
00178     
00179     /// isNull - Return true if the pointer held in the union is null,
00180     /// regardless of which type it is.
00181     bool isNull() const { return Val.isNull(); }
00182     operator bool() const { return !isNull(); }
00183     
00184     /// is<T>() return true if the Union currently holds the type matching T.
00185     template<typename T>
00186     int is() const {
00187       // Is it PT1/PT2?
00188       if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
00189         return Val.is<InnerUnion>() && Val.get<InnerUnion>().is<T>();
00190       return Val.is<T>();
00191     }
00192     
00193     /// get<T>() - Return the value of the specified pointer type. If the
00194     /// specified pointer type is incorrect, assert.
00195     template<typename T>
00196     T get() const {
00197       assert(is<T>() && "Invalid accessor called");
00198       // Is it PT1/PT2?
00199       if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
00200         return Val.get<InnerUnion>().get<T>();
00201       
00202       return Val.get<T>();
00203     }
00204     
00205     /// dyn_cast<T>() - If the current value is of the specified pointer type,
00206     /// return it, otherwise return null.
00207     template<typename T>
00208     T dyn_cast() const {
00209       if (is<T>()) return get<T>();
00210       return T();
00211     }
00212     
00213     /// Assignment operators - Allow assigning into this union from either
00214     /// pointer type, setting the discriminator to remember what it came from.
00215     const PointerUnion3 &operator=(const PT1 &RHS) {
00216       Val = InnerUnion(RHS);
00217       return *this;
00218     }
00219     const PointerUnion3 &operator=(const PT2 &RHS) {
00220       Val = InnerUnion(RHS);
00221       return *this;
00222     }
00223     const PointerUnion3 &operator=(const PT3 &RHS) {
00224       Val = RHS;
00225       return *this;
00226     }
00227     
00228     void *getOpaqueValue() const { return Val.getOpaqueValue(); }
00229     static PointerUnion3 getFromOpaqueValue(void *VP) {
00230       PointerUnion3 V;
00231       V.Val = ValTy::getFromOpaqueValue(VP);
00232       return V;
00233     }
00234   };
00235  
00236   // Teach SmallPtrSet that PointerUnion3 is "basically a pointer", that has
00237   // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
00238   template<typename PT1, typename PT2, typename PT3>
00239   class PointerLikeTypeTraits<PointerUnion3<PT1, PT2, PT3> > {
00240   public:
00241     static inline void *
00242     getAsVoidPointer(const PointerUnion3<PT1, PT2, PT3> &P) {
00243       return P.getOpaqueValue();
00244     }
00245     static inline PointerUnion3<PT1, PT2, PT3>
00246     getFromVoidPointer(void *P) {
00247       return PointerUnion3<PT1, PT2, PT3>::getFromOpaqueValue(P);
00248     }
00249     
00250     // The number of bits available are the min of the two pointer types.
00251     enum {
00252       NumLowBitsAvailable = 
00253         PointerLikeTypeTraits<typename PointerUnion3<PT1, PT2, PT3>::ValTy>
00254           ::NumLowBitsAvailable
00255     };
00256   };
00257 
00258   /// PointerUnion4 - This is a pointer union of four pointer types.  See
00259   /// documentation for PointerUnion for usage.
00260   template <typename PT1, typename PT2, typename PT3, typename PT4>
00261   class PointerUnion4 {
00262   public:
00263     typedef PointerUnion<PT1, PT2> InnerUnion1;
00264     typedef PointerUnion<PT3, PT4> InnerUnion2;
00265     typedef PointerUnion<InnerUnion1, InnerUnion2> ValTy;
00266   private:
00267     ValTy Val;
00268   public:
00269     PointerUnion4() {}
00270     
00271     PointerUnion4(PT1 V) {
00272       Val = InnerUnion1(V);
00273     }
00274     PointerUnion4(PT2 V) {
00275       Val = InnerUnion1(V);
00276     }
00277     PointerUnion4(PT3 V) {
00278       Val = InnerUnion2(V);
00279     }
00280     PointerUnion4(PT4 V) {
00281       Val = InnerUnion2(V);
00282     }
00283     
00284     /// isNull - Return true if the pointer held in the union is null,
00285     /// regardless of which type it is.
00286     bool isNull() const { return Val.isNull(); }
00287     operator bool() const { return !isNull(); }
00288     
00289     /// is<T>() return true if the Union currently holds the type matching T.
00290     template<typename T>
00291     int is() const {
00292       // Is it PT1/PT2?
00293       if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
00294         return Val.is<InnerUnion1>() && Val.get<InnerUnion1>().is<T>();
00295       return Val.is<InnerUnion2>() && Val.get<InnerUnion2>().is<T>();
00296     }
00297     
00298     /// get<T>() - Return the value of the specified pointer type. If the
00299     /// specified pointer type is incorrect, assert.
00300     template<typename T>
00301     T get() const {
00302       assert(is<T>() && "Invalid accessor called");
00303       // Is it PT1/PT2?
00304       if (::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0) != -1)
00305         return Val.get<InnerUnion1>().get<T>();
00306       
00307       return Val.get<InnerUnion2>().get<T>();
00308     }
00309     
00310     /// dyn_cast<T>() - If the current value is of the specified pointer type,
00311     /// return it, otherwise return null.
00312     template<typename T>
00313     T dyn_cast() const {
00314       if (is<T>()) return get<T>();
00315       return T();
00316     }
00317     
00318     /// Assignment operators - Allow assigning into this union from either
00319     /// pointer type, setting the discriminator to remember what it came from.
00320     const PointerUnion4 &operator=(const PT1 &RHS) {
00321       Val = InnerUnion1(RHS);
00322       return *this;
00323     }
00324     const PointerUnion4 &operator=(const PT2 &RHS) {
00325       Val = InnerUnion1(RHS);
00326       return *this;
00327     }
00328     const PointerUnion4 &operator=(const PT3 &RHS) {
00329       Val = InnerUnion2(RHS);
00330       return *this;
00331     }
00332     const PointerUnion4 &operator=(const PT4 &RHS) {
00333       Val = InnerUnion2(RHS);
00334       return *this;
00335     }
00336     
00337     void *getOpaqueValue() const { return Val.getOpaqueValue(); }
00338     static PointerUnion4 getFromOpaqueValue(void *VP) {
00339       PointerUnion4 V;
00340       V.Val = ValTy::getFromOpaqueValue(VP);
00341       return V;
00342     }
00343   };
00344   
00345   // Teach SmallPtrSet that PointerUnion4 is "basically a pointer", that has
00346   // # low bits available = min(PT1bits,PT2bits,PT2bits)-2.
00347   template<typename PT1, typename PT2, typename PT3, typename PT4>
00348   class PointerLikeTypeTraits<PointerUnion4<PT1, PT2, PT3, PT4> > {
00349   public:
00350     static inline void *
00351     getAsVoidPointer(const PointerUnion4<PT1, PT2, PT3, PT4> &P) {
00352       return P.getOpaqueValue();
00353     }
00354     static inline PointerUnion4<PT1, PT2, PT3, PT4>
00355     getFromVoidPointer(void *P) {
00356       return PointerUnion4<PT1, PT2, PT3, PT4>::getFromOpaqueValue(P);
00357     }
00358     
00359     // The number of bits available are the min of the two pointer types.
00360     enum {
00361       NumLowBitsAvailable = 
00362         PointerLikeTypeTraits<typename PointerUnion4<PT1, PT2, PT3, PT4>::ValTy>
00363           ::NumLowBitsAvailable
00364     };
00365   };
00366 }
00367 
00368 #endif