Skip to content

Commit b77b541

Browse files
authored
Merge pull request #13366 from davezarzycki/nfc_repack_misc_typebase_bits
2 parents 807142c + 780f41b commit b77b541

File tree

4 files changed

+66
-36
lines changed

4 files changed

+66
-36
lines changed

include/swift/AST/Types.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ class alignas(1 << TypeAlignInBits) TypeBase {
285285
};
286286
NUMBITS(ErrorType, NumTypeBaseBits + 1);
287287

288+
struct ParenTypeBitfields {
289+
unsigned : NumTypeBaseBits;
290+
291+
/// Whether there is an original type.
292+
enum { NumFlagBits = 5 };
293+
unsigned Flags : NumFlagBits;
294+
};
295+
NUMBITS(ParenType, NumTypeBaseBits + ParenTypeBitfields::NumFlagBits);
296+
288297
struct AnyFunctionTypeBitfields {
289298
unsigned : NumTypeBaseBits;
290299

@@ -309,7 +318,14 @@ class alignas(1 << TypeAlignInBits) TypeBase {
309318
unsigned : NumTypeBaseBits;
310319

311320
/// \brief The unique number assigned to this type variable.
312-
uint64_t ID : 64 - NumTypeBaseBits;
321+
uint64_t ID : 32 - NumTypeBaseBits;
322+
323+
/// Type variable options.
324+
unsigned Options : 3;
325+
326+
/// Index into the list of type variables, as used by the
327+
/// constraint graph.
328+
unsigned GraphIndex : 29;
313329
};
314330
NUMBITS(TypeVariableType, 64);
315331

@@ -353,6 +369,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
353369
union {
354370
TypeBaseBitfields TypeBaseBits;
355371
ErrorTypeBitfields ErrorTypeBits;
372+
ParenTypeBitfields ParenTypeBits;
356373
AnyFunctionTypeBitfields AnyFunctionTypeBits;
357374
TypeVariableTypeBitfields TypeVariableTypeBits;
358375
ArchetypeTypeBitfields ArchetypeTypeBits;
@@ -1415,6 +1432,9 @@ class ParameterTypeFlags {
14151432

14161433
public:
14171434
ParameterTypeFlags() = default;
1435+
static ParameterTypeFlags fromRaw(uint8_t raw) {
1436+
return ParameterTypeFlags(OptionSet<ParameterFlags>(raw));
1437+
}
14181438

14191439
ParameterTypeFlags(bool variadic, bool autoclosure, bool escaping, bool inOut, bool shared)
14201440
: value((variadic ? Variadic : 0) |
@@ -1465,7 +1485,6 @@ class ParameterTypeFlags {
14651485
/// ParenType - A paren type is a type that's been written in parentheses.
14661486
class ParenType : public TypeBase {
14671487
Type UnderlyingType;
1468-
ParameterTypeFlags parameterFlags;
14691488

14701489
friend class ASTContext;
14711490

@@ -1482,7 +1501,9 @@ class ParenType : public TypeBase {
14821501
TypeBase *getSinglyDesugaredType();
14831502

14841503
/// Get the parameter flags
1485-
ParameterTypeFlags getParameterFlags() const { return parameterFlags; }
1504+
ParameterTypeFlags getParameterFlags() const {
1505+
return ParameterTypeFlags::fromRaw(ParenTypeBits.Flags);
1506+
}
14861507

14871508
// Implement isa/cast/dyncast/etc.
14881509
static bool classof(const TypeBase *T) {

lib/AST/Type.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,8 @@ ParenType::ParenType(Type baseType, RecursiveTypeProperties properties,
13141314
: TypeBase(TypeKind::Paren, nullptr, properties),
13151315
UnderlyingType(flags.isInOut()
13161316
? InOutType::get(baseType)
1317-
: baseType),
1318-
parameterFlags(flags) {
1317+
: baseType) {
1318+
ParenTypeBits.Flags = flags.toRaw();
13191319
if (flags.isInOut())
13201320
assert(!baseType->is<InOutType>() && "caller did not pass a base type");
13211321
if (baseType->is<InOutType>())

lib/Sema/ConstraintSystem.h

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ enum TypeVariableOptions {
177177
/// to, what specific types it might be and, eventually, the fixed type to
178178
/// which it is assigned.
179179
class TypeVariableType::Implementation {
180-
/// Type variable options.
181-
unsigned Options : 3;
182-
183180
/// \brief The locator that describes where this type variable was generated.
184181
constraints::ConstraintLocator *locator;
185182

@@ -191,45 +188,53 @@ class TypeVariableType::Implementation {
191188
/// The corresponding node in the constraint graph.
192189
constraints::ConstraintGraphNode *GraphNode = nullptr;
193190

194-
/// Index into the list of type variables, as used by the
195-
/// constraint graph.
196-
unsigned GraphIndex;
197-
198191
friend class constraints::SavedTypeVariableBinding;
199192

200193
public:
194+
/// \brief Retrieve the type variable associated with this implementation.
195+
TypeVariableType *getTypeVariable() {
196+
return reinterpret_cast<TypeVariableType *>(this) - 1;
197+
}
198+
199+
/// \brief Retrieve the type variable associated with this implementation.
200+
const TypeVariableType *getTypeVariable() const {
201+
return reinterpret_cast<const TypeVariableType *>(this) - 1;
202+
}
203+
201204
explicit Implementation(constraints::ConstraintLocator *locator,
202205
unsigned options)
203-
: Options(options), locator(locator),
204-
ParentOrFixed(getTypeVariable()) { }
206+
: locator(locator), ParentOrFixed(getTypeVariable()) {
207+
getTypeVariable()->TypeVariableTypeBits.Options = options;
208+
}
205209

206210
/// \brief Retrieve the unique ID corresponding to this type variable.
207211
unsigned getID() const { return getTypeVariable()->getID(); }
208212

213+
unsigned getRawOptions() const {
214+
return getTypeVariable()->TypeVariableTypeBits.Options;
215+
}
216+
217+
void setRawOptions(unsigned bits) {
218+
getTypeVariable()->TypeVariableTypeBits.Options = bits;
219+
assert(getTypeVariable()->TypeVariableTypeBits.Options == bits
220+
&& "Trucation");
221+
}
222+
209223
/// Whether this type variable can bind to an lvalue type.
210-
bool canBindToLValue() const { return Options & TVO_CanBindToLValue; }
224+
bool canBindToLValue() const { return getRawOptions() & TVO_CanBindToLValue; }
211225

212226
/// Whether this type variable can bind to an inout type.
213-
bool canBindToInOut() const { return Options & TVO_CanBindToInOut; }
227+
bool canBindToInOut() const { return getRawOptions() & TVO_CanBindToInOut; }
214228

215229
/// Whether this type variable prefers a subtype binding over a supertype
216230
/// binding.
217231
bool prefersSubtypeBinding() const {
218-
return Options & TVO_PrefersSubtypeBinding;
232+
return getRawOptions() & TVO_PrefersSubtypeBinding;
219233
}
220234

221235
bool mustBeMaterializable() const {
222-
return !(Options & TVO_CanBindToInOut) && !(Options & TVO_CanBindToLValue);
223-
}
224-
225-
/// \brief Retrieve the type variable associated with this implementation.
226-
TypeVariableType *getTypeVariable() {
227-
return reinterpret_cast<TypeVariableType *>(this) - 1;
228-
}
229-
230-
/// \brief Retrieve the type variable associated with this implementation.
231-
const TypeVariableType *getTypeVariable() const {
232-
return reinterpret_cast<const TypeVariableType *>(this) - 1;
236+
return !(getRawOptions() & TVO_CanBindToInOut) &&
237+
!(getRawOptions() & TVO_CanBindToLValue);
233238
}
234239

235240
/// Retrieve the corresponding node in the constraint graph.
@@ -243,11 +248,13 @@ class TypeVariableType::Implementation {
243248
/// Retrieve the index into the constraint graph's list of type variables.
244249
unsigned getGraphIndex() const {
245250
assert(GraphNode && "Graph node isn't set");
246-
return GraphIndex;
251+
return getTypeVariable()->TypeVariableTypeBits.GraphIndex;
247252
}
248253

249254
/// Set the index into the constraint graph's list of type variables.
250-
void setGraphIndex(unsigned newIndex) { GraphIndex = newIndex; }
255+
void setGraphIndex(unsigned newIndex) {
256+
getTypeVariable()->TypeVariableTypeBits.GraphIndex = newIndex;
257+
}
251258

252259
/// \brief Check whether this type variable either has a representative that
253260
/// is not itself or has a fixed type binding.
@@ -339,8 +346,8 @@ class TypeVariableType::Implementation {
339346
if (!mustBeMaterializable() && otherRep->getImpl().mustBeMaterializable()) {
340347
if (record)
341348
recordBinding(*record);
342-
Options &= ~TVO_CanBindToLValue;
343-
Options &= ~TVO_CanBindToInOut;
349+
getTypeVariable()->TypeVariableTypeBits.Options &= ~TVO_CanBindToLValue;
350+
getTypeVariable()->TypeVariableTypeBits.Options &= ~TVO_CanBindToInOut;
344351
}
345352
}
346353

@@ -381,8 +388,10 @@ class TypeVariableType::Implementation {
381388
if (!rep->getImpl().mustBeMaterializable()) {
382389
if (record)
383390
rep->getImpl().recordBinding(*record);
384-
rep->getImpl().Options &= ~TVO_CanBindToLValue;
385-
rep->getImpl().Options &= ~TVO_CanBindToInOut;
391+
rep->getImpl().getTypeVariable()->TypeVariableTypeBits.Options
392+
&= ~TVO_CanBindToLValue;
393+
rep->getImpl().getTypeVariable()->TypeVariableTypeBits.Options
394+
&= ~TVO_CanBindToInOut;
386395
}
387396
}
388397

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ void TypeVariableType::Implementation::print(llvm::raw_ostream &OS) {
6161
}
6262

6363
SavedTypeVariableBinding::SavedTypeVariableBinding(TypeVariableType *typeVar)
64-
: TypeVarAndOptions(typeVar, typeVar->getImpl().Options),
64+
: TypeVarAndOptions(typeVar, typeVar->getImpl().getRawOptions()),
6565
ParentOrFixed(typeVar->getImpl().ParentOrFixed) { }
6666

6767
void SavedTypeVariableBinding::restore() {
6868
auto *typeVar = getTypeVariable();
69-
typeVar->getImpl().Options = getOptions();
69+
typeVar->getImpl().setRawOptions(getOptions());
7070
typeVar->getImpl().ParentOrFixed = ParentOrFixed;
7171
}
7272

0 commit comments

Comments
 (0)