Skip to content

[AST] NFC: Try to static_assert that ExtInfo and TypeBase agree #13079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ class alignas(1 << TypeAlignInBits) TypeBase {

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

struct SILFunctionTypeBitfields {
unsigned : NumTypeBaseBits;
unsigned ExtInfo : 6;
enum { NumExtInfoBits = 6 };
unsigned ExtInfo : NumExtInfoBits;
unsigned CalleeConvention : 3;
unsigned HasErrorResult : 1;
unsigned CoroutineKind : 2;
Expand Down Expand Up @@ -2392,20 +2394,21 @@ class AnyFunctionType : public TypeBase {
/// \brief A class which abstracts out some details necessary for
/// making a call.
class ExtInfo {
// NOTE: If bits are added or removed, then TypeBase::AnyFunctionTypeBits
// must be updated to match.

// If bits are added or removed, then TypeBase::AnyFunctionTypeBits
// and NumMaskBits must be updated, and they must match.
//
// |representation|isAutoClosure|noEscape|throws|
// | 0 .. 3 | 4 | 5 | 6 |
//
enum : unsigned {
RepresentationMask = 0x0F,
AutoClosureMask = 0x10,
NoEscapeMask = 0x20,
ThrowsMask = 0x40,
RepresentationMask = 0xF << 0,
AutoClosureMask = 1 << 4,
NoEscapeMask = 1 << 5,
ThrowsMask = 1 << 6,
NumMaskBits = 7
};

unsigned Bits;
unsigned Bits; // Naturally sized for speed.

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

Expand Down Expand Up @@ -2505,7 +2508,7 @@ class AnyFunctionType : public TypeBase {
return ExtInfo(Bits & ~ThrowsMask);
}

uint16_t getFuncAttrKey() const {
unsigned getFuncAttrKey() const {
return Bits;
}

Expand Down Expand Up @@ -2539,7 +2542,11 @@ class AnyFunctionType : public TypeBase {
: TypeBase(Kind, CanTypeContext, properties), Input(Input), Output(Output),
NumParams(NumParams) {
AnyFunctionTypeBits.ExtInfo = Info.Bits;
// The use of both assert() and static_assert() is intentional.
assert(AnyFunctionTypeBits.ExtInfo == Info.Bits && "Bits were dropped!");
static_assert(ExtInfo::NumMaskBits ==
AnyFunctionTypeBitfields::NumExtInfoBits,
"ExtInfo and AnyFunctionTypeBitfields must agree on bit size");
}

public:
Expand Down Expand Up @@ -3249,19 +3256,20 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
/// \brief A class which abstracts out some details necessary for
/// making a call.
class ExtInfo {
// NOTE: If bits are added or removed, then TypeBase::SILFunctionTypeBits
// must be updated to match.
// If bits are added or removed, then TypeBase::SILFunctionTypeBits
// and NumMaskBits must be updated, and they must match.

// |representation|pseudogeneric| noescape |
// | 0 .. 3 | 4 | 5 |
//
enum : unsigned {
RepresentationMask = 0x0F,
PseudogenericMask = 0x10,
NoEscapeMask = 0x20,
RepresentationMask = 0xF << 0,
PseudogenericMask = 1 << 4,
NoEscapeMask = 1 << 5,
NumMaskBits = 6
};

unsigned Bits;
unsigned Bits; // Naturally sized for speed.

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

Expand Down Expand Up @@ -3364,7 +3372,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
return ExtInfo(Bits & ~NoEscapeMask);
}

uint16_t getFuncAttrKey() const {
unsigned getFuncAttrKey() const {
return Bits;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3869,7 +3869,11 @@ SILFunctionType::SILFunctionType(GenericSignature *genericSig, ExtInfo ext,

SILFunctionTypeBits.HasErrorResult = errorResult.hasValue();
SILFunctionTypeBits.ExtInfo = ext.Bits;
// The use of both assert() and static_assert() below is intentional.
assert(SILFunctionTypeBits.ExtInfo == ext.Bits && "Bits were dropped!");
static_assert(ExtInfo::NumMaskBits ==
SILFunctionTypeBitfields::NumExtInfoBits,
"ExtInfo and SILFunctionTypeBitfields must agree on bit size");
SILFunctionTypeBits.CoroutineKind = unsigned(coroutineKind);
NumParameters = params.size();
if (coroutineKind == SILCoroutineKind::None) {
Expand Down