Implement PointerLikeTypeTraits for DeclGroupRef.
Make OpaquePtr work with things that are pointer-like but not
necessarily pointers.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67998 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/DeclGroup.h b/include/clang/AST/DeclGroup.h
index 3a57281..b1c2575 100644
--- a/include/clang/AST/DeclGroup.h
+++ b/include/clang/AST/DeclGroup.h
@@ -134,4 +134,21 @@
 };
   
 } // end clang namespace
+
+namespace llvm {
+  // DeclGroupRef is "like a pointer", implement PointerLikeTypeTraits.
+  template <typename T>
+  class PointerLikeTypeTraits;
+  template <>
+  class PointerLikeTypeTraits<clang::DeclGroupRef> {
+  public:
+    static inline void *getAsVoidPointer(clang::DeclGroupRef P) {
+      return P.getAsOpaquePtr();
+    }
+    static inline clang::DeclGroupRef getFromVoidPointer(void *P) {
+      return clang::DeclGroupRef::getFromOpaquePtr(P);
+    }
+    enum { NumLowBitsAvailable = 0 };
+  };
+}
 #endif
diff --git a/include/clang/Parse/Ownership.h b/include/clang/Parse/Ownership.h
index 6445254..b184abc 100644
--- a/include/clang/Parse/Ownership.h
+++ b/include/clang/Parse/Ownership.h
@@ -14,15 +14,12 @@
 #ifndef LLVM_CLANG_PARSE_OWNERSHIP_H
 #define LLVM_CLANG_PARSE_OWNERSHIP_H
 
+#include "llvm/Support/PointerLikeTypeTraits.h"
+
 //===----------------------------------------------------------------------===//
 // OpaquePtr
 //===----------------------------------------------------------------------===//
 
-namespace llvm {
-  template <typename T>
-  class PointerLikeTypeTraits;
-}
-
 namespace clang {
   /// OpaquePtr - This is a very simple POD type that wraps a pointer that the
   /// Parser doesn't know about but that Sema or another client does.  The UID
@@ -35,12 +32,26 @@
     OpaquePtr() : Ptr(0) {}
     
     template <typename T>
-    T* getAs() const { return static_cast<T*>(Ptr); }
+    T* getAs() const {
+      return llvm::PointerLikeTypeTraits<T*>::getFromVoidPointer(Ptr);
+    }
+    
+    template <typename T>
+    T getAsVal() const {
+      return llvm::PointerLikeTypeTraits<T>::getFromVoidPointer(Ptr);
+    }
     
     void *get() const { return Ptr; }
     
-    static OpaquePtr make(void *P) { OpaquePtr R; R.Ptr = P; return R; }
-    void set(void *P) { Ptr = P; }
+    template<typename T>
+    static OpaquePtr make(T P) {
+      OpaquePtr R; R.set(P); return R;
+    }
+    
+    template<typename T>
+    void set(T P) {
+      Ptr = llvm::PointerLikeTypeTraits<T>::getAsVoidPointer(P);
+    }
     
     operator bool() const { return Ptr != 0; }
   };