[llvm-commits] [llvm] r126019 - in /llvm/trunk/include/llvm/ADT: PointerIntPair.h PointerUnion.h

Argyrios Kyrtzidis akyrtzi at gmail.com
Fri Feb 18 19:55:58 PST 2011


Author: akirtzidis
Date: Fri Feb 18 21:55:58 2011
New Revision: 126019

URL: http://llvm.org/viewvc/llvm-project?rev=126019&view=rev
Log:
Allow getting the address of the value in a PointerUnion or PointerIntPair if one is
confident enough that he knows what he is doing.

Modified:
    llvm/trunk/include/llvm/ADT/PointerIntPair.h
    llvm/trunk/include/llvm/ADT/PointerUnion.h

Modified: llvm/trunk/include/llvm/ADT/PointerIntPair.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/PointerIntPair.h?rev=126019&r1=126018&r2=126019&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/PointerIntPair.h (original)
+++ llvm/trunk/include/llvm/ADT/PointerIntPair.h Fri Feb 18 21:55:58 2011
@@ -91,6 +91,13 @@
     Value |= IntVal << IntShift;  // Set new integer.
   }
 
+  PointerTy const *getAddrOfPointer() const {
+    assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&
+           "Can only return the address if IntBits is cleared and "
+           "PtrTraits doesn't change the pointer");
+    return reinterpret_cast<PointerTy const *>(&Value);
+  }
+
   void *getOpaqueValue() const { return reinterpret_cast<void*>(Value); }
   void setFromOpaqueValue(void *Val) { Value = reinterpret_cast<intptr_t>(Val);}
 

Modified: llvm/trunk/include/llvm/ADT/PointerUnion.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/PointerUnion.h?rev=126019&r1=126018&r2=126019&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/PointerUnion.h (original)
+++ llvm/trunk/include/llvm/ADT/PointerUnion.h Fri Feb 18 21:55:58 2011
@@ -107,6 +107,18 @@
       if (is<T>()) return get<T>();
       return T();
     }
+
+    /// \brief If the union is set to the first pointer type we can get an
+    /// address pointing to it.
+    template <typename T>
+    PT1 const *getAddrOf() const {
+      assert(is<PT1>() && "Val is not the first pointer");
+      assert(get<PT1>() == Val.getPointer() &&
+         "Can't get the address because PointerLikeTypeTraits changes the ptr");
+      T const *can_only_get_address_of_first_pointer_type
+                        = reinterpret_cast<PT1 const *>(Val.getAddrOfPointer());
+      return can_only_get_address_of_first_pointer_type;
+    }
     
     /// Assignment operators - Allow assigning into this union from either
     /// pointer type, setting the discriminator to remember what it came from.





More information about the llvm-commits mailing list