@@ -132,24 +132,34 @@ class PointerIntEnum {
132
132
static constexpr uintptr_t InvalidStorage = uintptr_t (0 ) - 1 ;
133
133
134
134
// / 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.
143
135
uintptr_t Storage;
144
136
145
137
public:
146
138
PointerIntEnum () : Storage(InvalidStorage) {}
147
139
140
+ // / Initialize this PointerIntEnum with the kind \p Kind and the Pointer \p
141
+ // / Ptr.
148
142
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);
150
150
}
151
151
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
+ }
153
163
154
164
PointerIntEnum (PointerIntEnum &&P) = default ;
155
165
PointerIntEnum (const PointerIntEnum &P) = default ;
@@ -168,7 +178,7 @@ class PointerIntEnum {
168
178
return !(*this == Other);
169
179
}
170
180
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
172
182
// / enum is invalid.
173
183
Optional<EnumTy> getKind () const {
174
184
if (!isValid ())
@@ -188,55 +198,26 @@ class PointerIntEnum {
188
198
return EnumTy (MaskedStorage);
189
199
}
190
200
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.
193
204
uintptr_t getIndex () const {
194
205
assert (isValid ());
195
206
assert (unsigned (*getKind ()) >= unsigned (EnumTy::FirstIndexKind));
196
207
return Storage >> IndexShiftOffset;
197
208
}
198
209
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.
199
212
PointerTy getPointer () const {
213
+ assert (isValid ());
214
+ assert (unsigned (*getKind ()) <= unsigned (EnumTy::LastPointerKind));
200
215
uintptr_t Value = Storage & ~(uintptr_t (EnumTy::FirstIndexKind));
201
216
return reinterpret_cast <PointerTy>(Value);
202
217
}
203
218
204
219
// / Return the raw storage of the type. Used for testing purposes.
205
220
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
- }
240
221
};
241
222
242
223
} // end swift namespace
0 commit comments