Add a simple type-safe bit-mangling pointer union class. This allows
[oota-llvm.git] / include / llvm / ADT / PointerUnion.h
1 //===- llvm/ADT/PointerUnion.h - Discriminated Union of 2 Ptrs --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PointerUnion class, which is a discriminated union of
11 // pointer types.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ADT_POINTERUNION_H
16 #define LLVM_ADT_POINTERUNION_H
17
18 #include "llvm/ADT/PointerIntPair.h"
19
20 namespace llvm {
21
22   /// getPointerUnionTypeNum - If the argument has type PT1* or PT2* return
23   /// false or true respectively.
24   template <typename PT1, typename PT2>
25   static inline bool getPointerUnionTypeNum(PT1 *P) { return false; }
26   template <typename PT1, typename PT2>
27   static inline bool getPointerUnionTypeNum(PT2 *P) { return true; }
28   // Enable, if we could use static_assert.
29   //template <typename PT1, typename PT2>
30   //static inline bool getPointerUnionTypeNum(...) { abort() }
31   
32   
33   /// Provide PointerLikeTypeTraits for void* that is used by PointerUnion
34   /// for the two template arguments.
35   template <typename PT1, typename PT2>
36   class PointerUnionUIntTraits {
37   public:
38     static inline void *getAsVoidPointer(void *P) { return P; }
39     static inline void *getFromVoidPointer(void *P) { return P; }
40     enum {
41       PT1BitsAv = PointerLikeTypeTraits<PT1>::NumLowBitsAvailable,
42       PT2BitsAv = PointerLikeTypeTraits<PT2>::NumLowBitsAvailable,
43       NumLowBitsAvailable = PT1BitsAv < PT2BitsAv ? PT1BitsAv : PT2BitsAv
44     };
45   };
46   
47   /// PointerUnion - This implements a discriminated union of two pointer types,
48   /// and keeps the discriminator bit-mangled into the low bits of the pointer.
49   /// This allows the implementation to be extremely efficient in space, but
50   /// permits a very natural and type-safe API.
51   ///
52   /// Common use patterns would be something like this:
53   ///    PointerUnion<int*, float*> P;
54   ///    P = (int*)0;
55   ///    printf("%d %d", P.is<int*>(), P.is<float*>());  // prints "1 0"
56   ///    X = P.get<int*>();     // ok.
57   ///    Y = P.get<float*>();   // runtime assertion failure.
58   ///    Z = P.get<double*>();  // does not compile.
59   ///    P = (float*)0;
60   ///    Y = P.get<float*>();   // ok.
61   ///    X = P.get<int*>();     // runtime assertion failure.
62   template <typename PT1, typename PT2>
63   class PointerUnion {
64   public:
65     typedef PointerIntPair<void*, 1, bool, 
66                            PointerUnionUIntTraits<PT1,PT2> > ValTy;
67   private:
68     ValTy Val;
69   public:
70     PointerUnion() {}
71     
72     PointerUnion(PT1 V) {
73       Val.setPointer(V);
74       Val.setInt(0);
75     }
76     PointerUnion(PT2 V) {
77       Val.setPointer(V);
78       Val.setInt(1);
79     }
80     
81     template<typename T>
82     int is() const {
83       return Val.getInt() == ::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0);
84     }
85     template<typename T>
86     T get() const {
87       assert(is<T>() && "Invalid accessor called");
88       return static_cast<T>(Val.getPointer());
89     }
90     
91     const PointerUnion &operator=(const PT1 &RHS) {
92       Val.setPointer(RHS);
93       Val.setInt(0);
94       return *this;
95     }
96     const PointerUnion &operator=(const PT2 &RHS) {
97       Val.setPointer(RHS);
98       Val.setInt(1);
99       return *this;
100     }
101     
102     void *getOpaqueValue() const { return Val.getOpaqueValue(); }
103     static PointerUnion getFromOpaqueValue(void *VP) {
104       PointerUnion V;
105       V.Val = ValTy::getFromOpaqueValue(VP);
106       return V;
107     }
108   };
109   
110   // Teach SmallPtrSet that PointerIntPair is "basically a pointer", that has
111   // # low bits available = min(PT1bits,PT2bits)-1.
112   template<typename PT1, typename PT2>
113   class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > {
114   public:
115     static inline void *
116     getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
117       return P.getOpaqueValue();
118     }
119     static inline PointerUnion<PT1, PT2>
120     getFromVoidPointer(void *P) {
121       return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
122     }
123     
124     // The number of bits available are the min of the two pointer types.
125     enum {
126       NumLowBitsAvailable = 
127         PointerUnion<PT1,PT2>::ValTy::NumLowBitsAvailable
128     };
129   };
130 }
131
132 #endif