From e9fb6b891ef6eccd28fb6fcff1f39b0aea2793e4 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Sat, 19 Feb 2011 03:55:58 +0000 Subject: [PATCH] Allow getting the address of the value in a PointerUnion or PointerIntPair if one is confident enough that he knows what he is doing. llvm-svn: 126019 --- llvm/include/llvm/ADT/PointerIntPair.h | 7 +++++++ llvm/include/llvm/ADT/PointerUnion.h | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/llvm/include/llvm/ADT/PointerIntPair.h b/llvm/include/llvm/ADT/PointerIntPair.h index 64f4a7cee4b9..85dbba2b4a4a 100644 --- a/llvm/include/llvm/ADT/PointerIntPair.h +++ b/llvm/include/llvm/ADT/PointerIntPair.h @@ -91,6 +91,13 @@ public: Value |= IntVal << IntShift; // Set new integer. } + PointerTy const *getAddrOfPointer() const { + assert(Value == reinterpret_cast(getPointer()) && + "Can only return the address if IntBits is cleared and " + "PtrTraits doesn't change the pointer"); + return reinterpret_cast(&Value); + } + void *getOpaqueValue() const { return reinterpret_cast(Value); } void setFromOpaqueValue(void *Val) { Value = reinterpret_cast(Val);} diff --git a/llvm/include/llvm/ADT/PointerUnion.h b/llvm/include/llvm/ADT/PointerUnion.h index 3a514b562697..61de042b0ff2 100644 --- a/llvm/include/llvm/ADT/PointerUnion.h +++ b/llvm/include/llvm/ADT/PointerUnion.h @@ -107,6 +107,18 @@ namespace llvm { if (is()) return get(); return T(); } + + /// \brief If the union is set to the first pointer type we can get an + /// address pointing to it. + template + PT1 const *getAddrOf() const { + assert(is() && "Val is not the first pointer"); + assert(get() == Val.getPointer() && + "Can't get the address because PointerLikeTypeTraits changes the ptr"); + T const *can_only_get_address_of_first_pointer_type + = reinterpret_cast(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.