Skip to content

Commit a52b07f

Browse files
committed
[AST] NFC: Try to static_assert that ExtInfo and TypeBase agree
1 parent a550042 commit a52b07f

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

include/swift/AST/Types.h

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
289289

290290
/// Extra information which affects how the function is called, like
291291
/// regparm and the calling convention.
292-
unsigned ExtInfo : 7;
292+
unsigned ExtInfo : 7; enum { NumExtInfoBits = 7 };
293293
};
294294
enum { NumAnyFunctionTypeBits = NumTypeBaseBits + 7 };
295295
static_assert(NumAnyFunctionTypeBits <= 32, "fits in an unsigned");
@@ -316,7 +316,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
316316

317317
struct SILFunctionTypeBitfields {
318318
unsigned : NumTypeBaseBits;
319-
unsigned ExtInfo : 6;
319+
unsigned ExtInfo : 6; enum { NumExtInfoBits = 6 };
320320
unsigned CalleeConvention : 3;
321321
unsigned HasErrorResult : 1;
322322
unsigned CoroutineKind : 2;
@@ -2392,20 +2392,21 @@ class AnyFunctionType : public TypeBase {
23922392
/// \brief A class which abstracts out some details necessary for
23932393
/// making a call.
23942394
class ExtInfo {
2395-
// NOTE: If bits are added or removed, then TypeBase::AnyFunctionTypeBits
2396-
// must be updated to match.
2397-
2395+
// If bits are added or removed, then TypeBase::AnyFunctionTypeBits
2396+
// and NumMaskBits must be updated, and they must match.
2397+
//
23982398
// |representation|isAutoClosure|noEscape|throws|
23992399
// | 0 .. 3 | 4 | 5 | 6 |
24002400
//
24012401
enum : unsigned {
2402-
RepresentationMask = 0x0F,
2403-
AutoClosureMask = 0x10,
2404-
NoEscapeMask = 0x20,
2405-
ThrowsMask = 0x40,
2402+
RepresentationMask = 0xF << 0,
2403+
AutoClosureMask = 1 << 4,
2404+
NoEscapeMask = 1 << 5,
2405+
ThrowsMask = 1 << 6,
2406+
NumMaskBits = 7
24062407
};
24072408

2408-
unsigned Bits;
2409+
unsigned Bits; // Naturally sized for speed.
24092410

24102411
ExtInfo(unsigned Bits) : Bits(Bits) {}
24112412

@@ -2505,7 +2506,7 @@ class AnyFunctionType : public TypeBase {
25052506
return ExtInfo(Bits & ~ThrowsMask);
25062507
}
25072508

2508-
uint16_t getFuncAttrKey() const {
2509+
unsigned getFuncAttrKey() const {
25092510
return Bits;
25102511
}
25112512

@@ -2539,7 +2540,11 @@ class AnyFunctionType : public TypeBase {
25392540
: TypeBase(Kind, CanTypeContext, properties), Input(Input), Output(Output),
25402541
NumParams(NumParams) {
25412542
AnyFunctionTypeBits.ExtInfo = Info.Bits;
2543+
// The use of both assert() and static_assert() is intentional.
25422544
assert(AnyFunctionTypeBits.ExtInfo == Info.Bits && "Bits were dropped!");
2545+
static_assert(ExtInfo::NumMaskBits ==
2546+
AnyFunctionTypeBitfields::NumExtInfoBits,
2547+
"ExtInfo and AnyFunctionTypeBitfields must agree on bit size");
25432548
}
25442549

25452550
public:
@@ -3249,19 +3254,20 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
32493254
/// \brief A class which abstracts out some details necessary for
32503255
/// making a call.
32513256
class ExtInfo {
3252-
// NOTE: If bits are added or removed, then TypeBase::SILFunctionTypeBits
3253-
// must be updated to match.
3257+
// If bits are added or removed, then TypeBase::SILFunctionTypeBits
3258+
// and NumMaskBits must be updated, and they must match.
32543259

32553260
// |representation|pseudogeneric| noescape |
32563261
// | 0 .. 3 | 4 | 5 |
32573262
//
32583263
enum : unsigned {
3259-
RepresentationMask = 0x0F,
3260-
PseudogenericMask = 0x10,
3261-
NoEscapeMask = 0x20,
3264+
RepresentationMask = 0xF << 0,
3265+
PseudogenericMask = 1 << 4,
3266+
NoEscapeMask = 1 << 5,
3267+
NumMaskBits = 6
32623268
};
32633269

3264-
unsigned Bits;
3270+
unsigned Bits; // Naturally sized for speed.
32653271

32663272
ExtInfo(unsigned Bits) : Bits(Bits) {}
32673273

@@ -3364,7 +3370,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
33643370
return ExtInfo(Bits & ~NoEscapeMask);
33653371
}
33663372

3367-
uint16_t getFuncAttrKey() const {
3373+
unsigned getFuncAttrKey() const {
33683374
return Bits;
33693375
}
33703376

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3869,7 +3869,11 @@ SILFunctionType::SILFunctionType(GenericSignature *genericSig, ExtInfo ext,
38693869

38703870
SILFunctionTypeBits.HasErrorResult = errorResult.hasValue();
38713871
SILFunctionTypeBits.ExtInfo = ext.Bits;
3872+
// The use of both assert() and static_assert() below is intentional.
38723873
assert(SILFunctionTypeBits.ExtInfo == ext.Bits && "Bits were dropped!");
3874+
static_assert(ExtInfo::NumMaskBits ==
3875+
SILFunctionTypeBitfields::NumExtInfoBits,
3876+
"ExtInfo and SILFunctionTypeBitfields must agree on bit size");
38733877
SILFunctionTypeBits.CoroutineKind = unsigned(coroutineKind);
38743878
NumParameters = params.size();
38753879
if (coroutineKind == SILCoroutineKind::None) {

0 commit comments

Comments
 (0)