Skip to content

Commit 3a937b4

Browse files
committed
[ptrintenum] Fix up some comments/inline some code/delete dead code.
1 parent 328c146 commit 3a937b4

File tree

1 file changed

+28
-47
lines changed

1 file changed

+28
-47
lines changed

include/swift/Basic/PointerIntEnum.h

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,34 @@ class PointerIntEnum {
132132
static constexpr uintptr_t InvalidStorage = uintptr_t(0) - 1;
133133

134134
/// The pointer sized type used for the actual storage.
135-
///
136-
/// Never access this directly. Instead use the following methods:
137-
///
138-
/// * getKind(): Same as RawKind except if the kind is LargeIndex, will
139-
/// discover the real underlying kind in the malloced memory.
140-
/// * getIndex(): Asserts if getKind() is a pointer storing kind.
141-
/// * getPointer(): Returns the underlying pointer cast into
142-
/// PointerTy. Asserts if getKind() is an index storing kind.
143135
uintptr_t Storage;
144136

145137
public:
146138
PointerIntEnum() : Storage(InvalidStorage) {}
147139

140+
/// Initialize this PointerIntEnum with the kind \p Kind and the Pointer \p
141+
/// Ptr.
148142
PointerIntEnum(EnumTy Kind, uintptr_t NewIndex) {
149-
initWithIndex(Kind, NewIndex);
143+
// If we can not represent this index, make the PointerIntEnum invalid.
144+
if (NewIndex > MaxIndex) {
145+
Storage = InvalidStorage;
146+
return;
147+
}
148+
149+
Storage = uintptr_t(Kind) | (uintptr_t(NewIndex) << IndexShiftOffset);
150150
}
151151

152-
PointerIntEnum(EnumTy Kind, PointerTy Ptr) { initWithPointer(Kind, Ptr); }
152+
/// Initialize this PointerIntEnum with the kind \p Kind and the Pointer \p
153+
/// Ptr.
154+
PointerIntEnum(EnumTy Kind, PointerTy Ptr) {
155+
// Make sure the pointer is at least aligned to NumPointerKindBits.
156+
assert((uintptr_t(Ptr) & ((1 << NumPointerKindBits) - 1)) == 0);
157+
// Make sure that Kind is a PointerKind.
158+
assert(unsigned(Kind) >= unsigned(EnumTy::FirstPointerKind));
159+
assert(unsigned(Kind) <= unsigned(EnumTy::LastPointerKind));
160+
161+
Storage = uintptr_t(Ptr) | uintptr_t(Kind);
162+
}
153163

154164
PointerIntEnum(PointerIntEnum &&P) = default;
155165
PointerIntEnum(const PointerIntEnum &P) = default;
@@ -168,7 +178,7 @@ class PointerIntEnum {
168178
return !(*this == Other);
169179
}
170180

171-
/// Convenience method for getting the kind of this enum. Returns None if this
181+
/// \returns the kind of the enum if the enum is valid. Returns None if the
172182
/// enum is invalid.
173183
Optional<EnumTy> getKind() const {
174184
if (!isValid())
@@ -188,55 +198,26 @@ class PointerIntEnum {
188198
return EnumTy(MaskedStorage);
189199
}
190200

191-
/// Convenience method for getting the underlying index. Assumes that this
192-
/// projection is valid. Otherwise it asserts.
201+
/// \returns the index stored in the enum if the enum has an index
202+
/// payload. Asserts if the PointerIntEnum is invalid or has a pointer
203+
/// payload.
193204
uintptr_t getIndex() const {
194205
assert(isValid());
195206
assert(unsigned(*getKind()) >= unsigned(EnumTy::FirstIndexKind));
196207
return Storage >> IndexShiftOffset;
197208
}
198209

210+
/// \returns the pointer stored in the enum if the enum has a pointer
211+
/// payload. Asserts if the PointerIntEnum is invalid or has a index payload.
199212
PointerTy getPointer() const {
213+
assert(isValid());
214+
assert(unsigned(*getKind()) <= unsigned(EnumTy::LastPointerKind));
200215
uintptr_t Value = Storage & ~(uintptr_t(EnumTy::FirstIndexKind));
201216
return reinterpret_cast<PointerTy>(Value);
202217
}
203218

204219
/// Return the raw storage of the type. Used for testing purposes.
205220
uintptr_t getStorage() const { return Storage; }
206-
207-
private:
208-
void initInvalid() { Storage = InvalidStorage; }
209-
210-
/// Initialize this PointerIntEnum with the kind \p Kind and the Pointer \p
211-
/// Ptr.
212-
///
213-
/// This is an internal helper routine that should not be used directly since
214-
/// it does not properly handle freeing memory.
215-
void initWithIndex(EnumTy Kind, uintptr_t NewIndex) {
216-
// If we can not represent this index, make the PointerIntEnum invalid.
217-
if (NewIndex > MaxIndex) {
218-
Storage = InvalidStorage;
219-
return;
220-
}
221-
222-
Storage = uintptr_t(Kind) | (uintptr_t(NewIndex) << IndexShiftOffset);
223-
}
224-
225-
/// Initialize this PointerIntEnum with the kind \p Kind and the Pointer \p
226-
/// Ptr.
227-
///
228-
/// This is an internal helper routine that should not be used directly since
229-
/// it does not properly handle freeing memory.
230-
void initWithPointer(EnumTy Kind, PointerTy Ptr) {
231-
// Make sure the pointer is at least aligned to NumPointerKindBits.
232-
assert((uintptr_t(Ptr) & ((1 << NumPointerKindBits) - 1)) == 0);
233-
// Make sure that Kind is a PointerKind.
234-
assert(unsigned(Kind) >= unsigned(EnumTy::FirstPointerKind));
235-
assert(unsigned(Kind) <= unsigned(EnumTy::LastPointerKind));
236-
237-
Storage = uintptr_t(Ptr);
238-
Storage |= uintptr_t(Kind);
239-
}
240221
};
241222

242223
} // end swift namespace

0 commit comments

Comments
 (0)