Skip to content

Commit aed3193

Browse files
authored
Merge pull request #13635 from davezarzycki/basic_nfc_inline_bitfield_cleanup
2 parents 33164cc + da23853 commit aed3193

File tree

8 files changed

+78
-203
lines changed

8 files changed

+78
-203
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ bool conflicting(const OverloadSignature& sig1, const OverloadSignature& sig2);
236236

237237
/// Decl - Base class for all declarations in Swift.
238238
class alignas(1 << DeclAlignInBits) Decl {
239+
protected:
240+
union { uint64_t OpaqueBits;
241+
239242
SWIFT_INLINE_BITFIELD_BASE(Decl, bitmax(NumDeclKindBits,8)+1+1+1+1+1+1+1,
240243
Kind : bitmax(NumDeclKindBits,8),
241244

@@ -587,34 +590,6 @@ class alignas(1 << DeclAlignInBits) Decl {
587590
NumberOfVTableEntries : 2
588591
);
589592

590-
protected:
591-
union {
592-
uint64_t OpaqueBits;
593-
SWIFT_INLINE_BITS(Decl);
594-
SWIFT_INLINE_BITS(PatternBindingDecl);
595-
SWIFT_INLINE_BITS(EnumCaseDecl);
596-
SWIFT_INLINE_BITS(ValueDecl);
597-
SWIFT_INLINE_BITS(AbstractStorageDecl);
598-
SWIFT_INLINE_BITS(AbstractFunctionDecl);
599-
SWIFT_INLINE_BITS(VarDecl);
600-
SWIFT_INLINE_BITS(ParamDecl);
601-
SWIFT_INLINE_BITS(EnumElementDecl);
602-
SWIFT_INLINE_BITS(FuncDecl);
603-
SWIFT_INLINE_BITS(ConstructorDecl);
604-
SWIFT_INLINE_BITS(TypeDecl);
605-
SWIFT_INLINE_BITS(GenericTypeParamDecl);
606-
SWIFT_INLINE_BITS(TypeAliasDecl);
607-
SWIFT_INLINE_BITS(NominalTypeDecl);
608-
SWIFT_INLINE_BITS(ProtocolDecl);
609-
SWIFT_INLINE_BITS(ClassDecl);
610-
SWIFT_INLINE_BITS(StructDecl);
611-
SWIFT_INLINE_BITS(EnumDecl);
612-
SWIFT_INLINE_BITS(AssociatedTypeDecl);
613-
SWIFT_INLINE_BITS(PrecedenceGroupDecl);
614-
SWIFT_INLINE_BITS(ImportDecl);
615-
SWIFT_INLINE_BITS(ExtensionDecl);
616-
SWIFT_INLINE_BITS(IfConfigDecl);
617-
SWIFT_INLINE_BITS(MissingMemberDecl);
618593
} Bits;
619594

620595
// Storage for the declaration attributes.

include/swift/AST/Expr.h

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ class alignas(8) Expr {
130130
Expr(const Expr&) = delete;
131131
void operator=(const Expr&) = delete;
132132

133+
protected:
134+
union { uint64_t OpaqueBits;
135+
133136
SWIFT_INLINE_BITFIELD_BASE(Expr, bitmax(NumExprKindBits,8)+2+1,
134137
/// The subclass of Expr that this is.
135138
Kind : bitmax(NumExprKindBits,8),
@@ -363,41 +366,6 @@ class alignas(8) Expr {
363366
NumElements : 32
364367
);
365368

366-
protected:
367-
union {
368-
SWIFT_INLINE_BITS(Expr);
369-
SWIFT_INLINE_BITS(NumberLiteralExpr);
370-
SWIFT_INLINE_BITS(StringLiteralExpr);
371-
SWIFT_INLINE_BITS(DeclRefExpr);
372-
SWIFT_INLINE_BITS(UnresolvedDeclRefExpr);
373-
SWIFT_INLINE_BITS(TupleExpr);
374-
SWIFT_INLINE_BITS(TupleElementExpr);
375-
SWIFT_INLINE_BITS(MemberRefExpr);
376-
SWIFT_INLINE_BITS(UnresolvedDotExpr);
377-
SWIFT_INLINE_BITS(SubscriptExpr);
378-
SWIFT_INLINE_BITS(DynamicSubscriptExpr);
379-
SWIFT_INLINE_BITS(UnresolvedMemberExpr);
380-
SWIFT_INLINE_BITS(OverloadSetRefExpr);
381-
SWIFT_INLINE_BITS(BooleanLiteralExpr);
382-
SWIFT_INLINE_BITS(MagicIdentifierLiteralExpr);
383-
SWIFT_INLINE_BITS(ObjectLiteralExpr);
384-
SWIFT_INLINE_BITS(AbstractClosureExpr);
385-
SWIFT_INLINE_BITS(ClosureExpr);
386-
SWIFT_INLINE_BITS(BindOptionalExpr);
387-
SWIFT_INLINE_BITS(ApplyExpr);
388-
SWIFT_INLINE_BITS(CallExpr);
389-
SWIFT_INLINE_BITS(CheckedCastExpr);
390-
SWIFT_INLINE_BITS(TupleShuffleExpr);
391-
SWIFT_INLINE_BITS(InOutToPointerExpr);
392-
SWIFT_INLINE_BITS(ArrayToPointerExpr);
393-
SWIFT_INLINE_BITS(ObjCSelectorExpr);
394-
SWIFT_INLINE_BITS(KeyPathExpr);
395-
SWIFT_INLINE_BITS(ParenExpr);
396-
SWIFT_INLINE_BITS(SequenceExpr);
397-
SWIFT_INLINE_BITS(CollectionExpr);
398-
SWIFT_INLINE_BITS(ErasureExpr);
399-
SWIFT_INLINE_BITS(UnresolvedSpecializeExpr);
400-
SWIFT_INLINE_BITS(CaptureListExpr);
401369
} Bits;
402370

403371
private:
@@ -410,6 +378,7 @@ class alignas(8) Expr {
410378

411379
protected:
412380
Expr(ExprKind Kind, bool Implicit, Type Ty = Type()) : Ty(Ty) {
381+
Bits.OpaqueBits = 0;
413382
Bits.Expr.Kind = unsigned(Kind);
414383
Bits.Expr.Implicit = Implicit;
415384
Bits.Expr.LValueAccessKind = 0;

include/swift/AST/Pattern.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, PatternKind kind);
4949

5050
/// Pattern - Base class for all patterns in Swift.
5151
class alignas(8) Pattern {
52+
protected:
53+
union { uint64_t OpaqueBits;
54+
5255
SWIFT_INLINE_BITFIELD_BASE(Pattern, bitmax(NumPatternKindBits,8)+1+1,
5356
Kind : bitmax(NumPatternKindBits,8),
5457
isImplicit : 1,
@@ -64,12 +67,6 @@ class alignas(8) Pattern {
6467
IsPropagatedType : 1
6568
);
6669

67-
protected:
68-
union {
69-
uint64_t OpaqueBits;
70-
SWIFT_INLINE_BITS(Pattern);
71-
SWIFT_INLINE_BITS(TuplePattern);
72-
SWIFT_INLINE_BITS(TypedPattern);
7370
} Bits;
7471

7572
Pattern(PatternKind kind) {

include/swift/AST/Stmt.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class alignas(8) Stmt {
5151
Stmt(const Stmt&) = delete;
5252
Stmt& operator=(const Stmt&) = delete;
5353

54+
protected:
55+
union { uint64_t OpaqueBits;
56+
5457
SWIFT_INLINE_BITFIELD_BASE(Stmt, bitmax(NumStmtKindBits,8) + 1,
5558
/// Kind - The subclass of Stmt that this is.
5659
Kind : bitmax(NumStmtKindBits,8),
@@ -81,14 +84,6 @@ class alignas(8) Stmt {
8184
CaseCount : 32
8285
);
8386

84-
protected:
85-
union {
86-
uint64_t OpaqueBits;
87-
SWIFT_INLINE_BITS(Stmt);
88-
SWIFT_INLINE_BITS(BraceStmt);
89-
SWIFT_INLINE_BITS(DoCatchStmt);
90-
SWIFT_INLINE_BITS(CaseStmt);
91-
SWIFT_INLINE_BITS(SwitchStmt);
9287
} Bits;
9388

9489
/// Return the given value for the 'implicit' flag if present, or if None,

include/swift/AST/TypeRepr.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class alignas(8) TypeRepr {
4949
TypeRepr(const TypeRepr&) = delete;
5050
void operator=(const TypeRepr&) = delete;
5151

52+
protected:
53+
union { uint64_t OpaqueBits;
54+
5255
SWIFT_INLINE_BITFIELD_BASE(TypeRepr, bitmax(NumTypeReprKindBits,8)+1+1,
5356
/// The subclass of TypeRepr that this is.
5457
Kind : bitmax(NumTypeReprKindBits,8),
@@ -71,11 +74,6 @@ class alignas(8) TypeRepr {
7174
NumElements : 16
7275
);
7376

74-
protected:
75-
union {
76-
uint64_t OpaqueBits;
77-
SWIFT_INLINE_BITS(TypeRepr);
78-
SWIFT_INLINE_BITS(TupleTypeRepr);
7977
} Bits;
8078

8179
TypeRepr(TypeReprKind K) {

include/swift/AST/Types.h

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
259259
/// form of a non-canonical type is requested.
260260
llvm::PointerUnion<TypeBase *, const ASTContext *> CanonicalType;
261261

262-
SWIFT_INLINE_BITFIELD_BASE(TypeBase, bitmax(NumTypeKindBits,8) +
263-
RecursiveTypeProperties::BitWidth,
264-
/// Kind - The discriminator that indicates what subclass of type this is.
265-
Kind : bitmax(NumTypeKindBits,8),
266-
267-
Properties : RecursiveTypeProperties::BitWidth
268-
);
269-
270262
/// Returns true if the given type is a sugared type.
271263
///
272264
/// Only intended for use in compile-time assertions.
@@ -277,6 +269,18 @@ class alignas(1 << TypeAlignInBits) TypeBase {
277269
}
278270

279271
protected:
272+
enum { NumAFTExtInfoBits = 7 };
273+
enum { NumSILExtInfoBits = 6 };
274+
union { uint64_t OpaqueBits;
275+
276+
SWIFT_INLINE_BITFIELD_BASE(TypeBase, bitmax(NumTypeKindBits,8) +
277+
RecursiveTypeProperties::BitWidth,
278+
/// Kind - The discriminator that indicates what subclass of type this is.
279+
Kind : bitmax(NumTypeKindBits,8),
280+
281+
Properties : RecursiveTypeProperties::BitWidth
282+
);
283+
280284
SWIFT_INLINE_BITFIELD(ErrorType, TypeBase, 1,
281285
/// Whether there is an original type.
282286
HasOriginalType : 1
@@ -288,7 +292,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
288292
Flags : NumFlagBits
289293
);
290294

291-
enum { NumAFTExtInfoBits = 7 };
292295
SWIFT_INLINE_BITFIELD_FULL(AnyFunctionType, TypeBase, NumAFTExtInfoBits+16,
293296
/// Extra information which affects how the function is called, like
294297
/// regparm and the calling convention.
@@ -318,7 +321,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
318321
GraphIndex : 29
319322
);
320323

321-
enum { NumSILExtInfoBits = 6 };
322324
SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+3+1+2,
323325
ExtInfo : NumSILExtInfoBits,
324326
CalleeConvention : 3,
@@ -367,20 +369,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
367369
GenericArgCount : 32
368370
);
369371

370-
union {
371-
uint64_t OpaqueBits;
372-
SWIFT_INLINE_BITS(TypeBase);
373-
SWIFT_INLINE_BITS(ErrorType);
374-
SWIFT_INLINE_BITS(ParenType);
375-
SWIFT_INLINE_BITS(AnyFunctionType);
376-
SWIFT_INLINE_BITS(TypeVariableType);
377-
SWIFT_INLINE_BITS(ArchetypeType);
378-
SWIFT_INLINE_BITS(SILFunctionType);
379-
SWIFT_INLINE_BITS(SILBoxType);
380-
SWIFT_INLINE_BITS(AnyMetatypeType);
381-
SWIFT_INLINE_BITS(ProtocolCompositionType);
382-
SWIFT_INLINE_BITS(TupleType);
383-
SWIFT_INLINE_BITS(BoundGenericType);
384372
} Bits;
385373

386374
protected:

include/swift/Basic/InlineBitfield.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ namespace swift {
3838
class T##Bitfield { \
3939
friend class T; \
4040
uint64_t __VA_ARGS__; \
41-
uint64_t : 64 - (C); /* Pad and error if C > 64 */ \
42-
}; \
41+
} T; \
4342
LLVM_PACKED_END \
44-
enum { Num##T##Bits = (C) }
43+
enum { Num##T##Bits = (C) }; \
44+
static_assert(sizeof(T##Bitfield) <= 8, "Bitfield overflow")
4545

4646
/// Define an bitfield for type 'T' with parent class 'U' and 'C' bits used.
4747
#define SWIFT_INLINE_BITFIELD(T, U, C, ...) \
4848
LLVM_PACKED_START \
4949
class T##Bitfield { \
5050
friend class T; \
5151
uint64_t : Num##U##Bits, __VA_ARGS__; \
52-
uint64_t : 64 - (Num##U##Bits + (C)); /* Pad and error if C > 64 */ \
53-
}; \
52+
} T; \
5453
LLVM_PACKED_END \
55-
enum { Num##T##Bits = Num##U##Bits + (C) }
54+
enum { Num##T##Bits = Num##U##Bits + (C) }; \
55+
static_assert(sizeof(T##Bitfield) <= 8, "Bitfield overflow")
5656

5757
/// Define a full bitfield for type 'T' that uses all of the remaining bits in
5858
/// the inline bitfield.
@@ -72,16 +72,14 @@ namespace swift {
7272
friend class T; \
7373
enum { NumPadBits = 64 - (Num##U##Bits + (C)) }; \
7474
uint64_t : Num##U##Bits, __VA_ARGS__; \
75-
}; \
75+
} T; \
7676
LLVM_PACKED_END \
7777
static_assert(sizeof(T##Bitfield) <= 8, "Bitfield overflow")
7878

7979
/// Define an empty bitfield for type 'T'.
8080
#define SWIFT_INLINE_BITFIELD_EMPTY(T, U) \
8181
enum { Num##T##Bits = Num##U##Bits }
8282

83-
#define SWIFT_INLINE_BITS(T) T##Bitfield T
84-
8583
// XXX/HACK: templated max() doesn't seem to work in a bitfield size context.
8684
constexpr unsigned bitmax(unsigned a, unsigned b) {
8785
return a > b ? a : b;

0 commit comments

Comments
 (0)