PointerUnion.h revision 3a9fe06bfe30da6fb1e0a540f05877c3640c7335
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
20namespace 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    /// isNull - Return true if the pointer help in the union is null,
82    /// regardless of which type it is.
83    bool isNull() const { return Val.getPointer() == 0; }
84
85    /// is<T>() return true if the Union currently holds the type matching T.
86    template<typename T>
87    int is() const {
88      return Val.getInt() == ::llvm::getPointerUnionTypeNum<PT1, PT2>((T*)0);
89    }
90
91    /// get<T>() - Return the value of the specified pointer type. If the
92    /// specified pointer type is incorrect, assert.
93    template<typename T>
94    T get() const {
95      assert(is<T>() && "Invalid accessor called");
96      return static_cast<T>(Val.getPointer());
97    }
98
99    /// dyn_cast<T>() - If the current value is of the specified pointer type,
100    /// return it, otherwise return null.
101    template<typename T>
102    T dyn_cast() const {
103      if (is<T>()) return static_cast<T>(Val.getPointer());
104      return T();
105    }
106
107    /// Assignment operators - Allow assigning into this union from either
108    /// pointer type, setting the discriminator to remember what it came from.
109    const PointerUnion &operator=(const PT1 &RHS) {
110      Val.setPointer(RHS);
111      Val.setInt(0);
112      return *this;
113    }
114    const PointerUnion &operator=(const PT2 &RHS) {
115      Val.setPointer(RHS);
116      Val.setInt(1);
117      return *this;
118    }
119
120    void *getOpaqueValue() const { return Val.getOpaqueValue(); }
121    static PointerUnion getFromOpaqueValue(void *VP) {
122      PointerUnion V;
123      V.Val = ValTy::getFromOpaqueValue(VP);
124      return V;
125    }
126  };
127
128  // Teach SmallPtrSet that PointerIntPair is "basically a pointer", that has
129  // # low bits available = min(PT1bits,PT2bits)-1.
130  template<typename PT1, typename PT2>
131  class PointerLikeTypeTraits<PointerUnion<PT1, PT2> > {
132  public:
133    static inline void *
134    getAsVoidPointer(const PointerUnion<PT1, PT2> &P) {
135      return P.getOpaqueValue();
136    }
137    static inline PointerUnion<PT1, PT2>
138    getFromVoidPointer(void *P) {
139      return PointerUnion<PT1, PT2>::getFromOpaqueValue(P);
140    }
141
142    // The number of bits available are the min of the two pointer types.
143    enum {
144      NumLowBitsAvailable =
145        PointerUnion<PT1,PT2>::ValTy::NumLowBitsAvailable
146    };
147  };
148}
149
150#endif
151