diff options
Diffstat (limited to 'include/llvm/ADT/TinyPtrVector.h')
-rw-r--r-- | include/llvm/ADT/TinyPtrVector.h | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/include/llvm/ADT/TinyPtrVector.h b/include/llvm/ADT/TinyPtrVector.h index 15137f5ebf8c8..f29608f3d3d1e 100644 --- a/include/llvm/ADT/TinyPtrVector.h +++ b/include/llvm/ADT/TinyPtrVector.h @@ -27,9 +27,12 @@ class TinyPtrVector { public: typedef llvm::SmallVector<EltTy, 4> VecTy; typedef typename VecTy::value_type value_type; + typedef llvm::PointerUnion<EltTy, VecTy *> PtrUnion; - llvm::PointerUnion<EltTy, VecTy*> Val; +private: + PtrUnion Val; +public: TinyPtrVector() {} ~TinyPtrVector() { if (VecTy *V = Val.template dyn_cast<VecTy*>()) @@ -96,12 +99,13 @@ public: return *this; } - /// Constructor from a single element. - explicit TinyPtrVector(EltTy Elt) : Val(Elt) {} - /// Constructor from an ArrayRef. + /// + /// This also is a constructor for individual array elements due to the single + /// element constructor for ArrayRef. explicit TinyPtrVector(ArrayRef<EltTy> Elts) - : Val(new VecTy(Elts.begin(), Elts.end())) {} + : Val(Elts.size() == 1 ? PtrUnion(Elts[0]) + : PtrUnion(new VecTy(Elts.begin(), Elts.end()))) {} // implicit conversion operator to ArrayRef. operator ArrayRef<EltTy>() const { @@ -112,6 +116,15 @@ public: return *Val.template get<VecTy*>(); } + // implicit conversion operator to MutableArrayRef. + operator MutableArrayRef<EltTy>() { + if (Val.isNull()) + return None; + if (Val.template is<EltTy>()) + return *Val.getAddrOfPtr1(); + return *Val.template get<VecTy*>(); + } + bool empty() const { // This vector can be empty if it contains no element, or if it // contains a pointer to an empty vector. |