Skip to content

Commit

Permalink
Allow getting the address of the value in a PointerUnion or PointerIn…
Browse files Browse the repository at this point in the history
…tPair if one is

confident enough that he knows what he is doing.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126019 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
akyrtzi committed Feb 19, 2011
1 parent dd6e40a commit 38297f5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/llvm/ADT/PointerIntPair.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ class PointerIntPair {
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);}

Expand Down
12 changes: 12 additions & 0 deletions include/llvm/ADT/PointerUnion.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ namespace llvm {
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.
Expand Down

0 comments on commit 38297f5

Please sign in to comment.