Skip to content

Commit 3b2b431

Browse files
committed
[ptrintenum] Use PointerLikeTypeTraits to convert PointerTy <-> (void *).
This will enable PointerIntEnum to be used with PointerLikeTypes that are not pointers (for instance SILValue).
1 parent 3a937b4 commit 3b2b431

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

include/swift/Basic/PointerIntEnum.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,21 @@ class PointerIntEnum {
146146
return;
147147
}
148148

149-
Storage = uintptr_t(Kind) | (uintptr_t(NewIndex) << IndexShiftOffset);
149+
Storage = uintptr_t(Kind) | (NewIndex << IndexShiftOffset);
150150
}
151151

152152
/// Initialize this PointerIntEnum with the kind \p Kind and the Pointer \p
153153
/// Ptr.
154154
PointerIntEnum(EnumTy Kind, PointerTy Ptr) {
155+
void *VoidPtr = PtrTraits::getAsVoidPointer(Ptr);
156+
155157
// Make sure the pointer is at least aligned to NumPointerKindBits.
156-
assert((uintptr_t(Ptr) & ((1 << NumPointerKindBits) - 1)) == 0);
158+
assert((uintptr_t(VoidPtr) & ((1 << NumPointerKindBits) - 1)) == 0);
157159
// Make sure that Kind is a PointerKind.
158160
assert(unsigned(Kind) >= unsigned(EnumTy::FirstPointerKind));
159161
assert(unsigned(Kind) <= unsigned(EnumTy::LastPointerKind));
160162

161-
Storage = uintptr_t(Ptr) | uintptr_t(Kind);
163+
Storage = uintptr_t(VoidPtr) | uintptr_t(Kind);
162164
}
163165

164166
PointerIntEnum(PointerIntEnum &&P) = default;
@@ -213,7 +215,7 @@ class PointerIntEnum {
213215
assert(isValid());
214216
assert(unsigned(*getKind()) <= unsigned(EnumTy::LastPointerKind));
215217
uintptr_t Value = Storage & ~(uintptr_t(EnumTy::FirstIndexKind));
216-
return reinterpret_cast<PointerTy>(Value);
218+
return PtrTraits::getFromVoidPointer((void *)(Value));
217219
}
218220

219221
/// Return the raw storage of the type. Used for testing purposes.

0 commit comments

Comments
 (0)