Skip to content

Commit e60daaa

Browse files
authored
[AST] Pack bitfields in TypeRepr (#5981)
1 parent 2840248 commit e60daaa

File tree

2 files changed

+61
-33
lines changed

2 files changed

+61
-33
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,60 @@ class alignas(8) TypeRepr {
4545
TypeRepr(const TypeRepr&) = delete;
4646
void operator=(const TypeRepr&) = delete;
4747

48-
/// \brief The subclass of TypeRepr that this is.
49-
unsigned Kind : 6;
48+
class TypeReprBitfields {
49+
friend class TypeRepr;
50+
/// The subclass of TypeRepr that this is.
51+
unsigned Kind : 6;
52+
53+
/// Whether this type representation is known to contain an invalid
54+
/// type.
55+
unsigned Invalid : 1;
56+
57+
/// Whether this type representation had a warning emitted related to it.
58+
/// This is a hack related to how we resolve type exprs multiple times in
59+
/// generic contexts.
60+
unsigned Warned : 1;
61+
};
62+
enum { NumTypeReprBits = 8 };
63+
class TupleTypeReprBitfields {
64+
friend class TupleTypeRepr;
65+
unsigned : NumTypeReprBits;
66+
// HasNames, HasLabels?
67+
unsigned NameStatus : 2;
68+
// Whether this tuple has '...' and its position.
69+
unsigned HasEllipsis : 1;
70+
};
5071

51-
/// Whether this type representation is known to contain an invalid
52-
/// type.
53-
unsigned Invalid : 1;
72+
protected:
73+
union {
74+
TypeReprBitfields TypeReprBits;
75+
TupleTypeReprBitfields TupleTypeReprBits;
76+
};
5477

55-
/// Whether this type representation had a warning emitted related to it.
56-
/// This is a hack related to how we resolve type exprs multiple times in
57-
/// generic contexts.
58-
unsigned Warned : 1;
78+
TypeRepr(TypeReprKind K) {
79+
TypeReprBits.Kind = static_cast<unsigned>(K);
80+
TypeReprBits.Invalid = false;
81+
TypeReprBits.Warned = false;
82+
}
5983

84+
private:
6085
SourceLoc getLocImpl() const { return getStartLoc(); }
6186

62-
protected:
63-
TypeRepr(TypeReprKind K)
64-
: Kind(static_cast<unsigned>(K)), Invalid(false), Warned(false) {}
65-
6687
public:
67-
TypeReprKind getKind() const { return static_cast<TypeReprKind>(Kind); }
88+
TypeReprKind getKind() const {
89+
return static_cast<TypeReprKind>(TypeReprBits.Kind);
90+
}
6891

6992
/// Is this type representation known to be invalid?
70-
bool isInvalid() const { return Invalid; }
93+
bool isInvalid() const { return TypeReprBits.Invalid; }
7194

7295
/// Note that this type representation describes an invalid type.
73-
void setInvalid() { Invalid = true; }
96+
void setInvalid() { TypeReprBits.Invalid = true; }
7497

7598
/// If a warning is produced about this type repr, keep track of that so we
7699
/// don't emit another one upon further reanalysis.
77-
bool isWarnedAbout() const { return Warned; }
78-
void setWarned() { Warned = true; }
100+
bool isWarnedAbout() const { return TypeReprBits.Warned; }
101+
void setWarned() { TypeReprBits.Warned = true; }
79102

80103
/// Get the representative location for pointing at this type.
81104
SourceLoc getLoc() const;
@@ -556,21 +579,21 @@ class TupleTypeRepr final : public TypeRepr,
556579
NotNamed = 0,
557580
HasNames = 1,
558581
HasLabels = 2
559-
} NameStatus : 2;
560-
bool HasEllipsis : 1;
582+
};
561583

562584
size_t numTrailingObjects(OverloadToken<TypeRepr *>) const {
563585
return NumElements;
564586
}
565587
size_t numTrailingObjects(OverloadToken<Identifier>) const {
566-
return NameStatus >= HasNames ? NumElements : 0;
588+
return TupleTypeReprBits.NameStatus >= HasNames ? NumElements : 0;
567589
}
568590
size_t numTrailingObjects(OverloadToken<SourceLoc>) const {
569-
switch (NameStatus) {
591+
switch (TupleTypeReprBits.NameStatus) {
570592
case NotNamed: return 0;
571593
case HasNames: return NumElements;
572594
case HasLabels: return NumElements + NumElements;
573595
}
596+
llvm_unreachable("all cases should have been handled");
574597
}
575598

576599
TupleTypeRepr(ArrayRef<TypeRepr *> Elements, SourceRange Parens,
@@ -581,8 +604,12 @@ class TupleTypeRepr final : public TypeRepr,
581604
public:
582605

583606
unsigned getNumElements() const { return NumElements; }
584-
bool hasElementNames() const { return NameStatus >= HasNames; }
585-
bool hasUnderscoreLocs() const { return NameStatus == HasLabels; }
607+
bool hasElementNames() const {
608+
return TupleTypeReprBits.NameStatus >= HasNames;
609+
}
610+
bool hasUnderscoreLocs() const {
611+
return TupleTypeReprBits.NameStatus == HasLabels;
612+
}
586613

587614
ArrayRef<TypeRepr *> getElements() const {
588615
return { getTrailingObjects<TypeRepr *>(), NumElements };
@@ -622,19 +649,19 @@ class TupleTypeRepr final : public TypeRepr,
622649
}
623650

624651
SourceRange getParens() const { return Parens; }
652+
653+
bool hasEllipsis() const { return TupleTypeReprBits.HasEllipsis; }
625654
SourceLoc getEllipsisLoc() const {
626-
return HasEllipsis ?
655+
return hasEllipsis() ?
627656
getTrailingObjects<SourceLocAndIdx>()[0].first : SourceLoc();
628657
}
629658
unsigned getEllipsisIndex() const {
630-
return HasEllipsis ?
659+
return hasEllipsis() ?
631660
getTrailingObjects<SourceLocAndIdx>()[0].second : NumElements;
632661
}
633-
bool hasEllipsis() const { return HasEllipsis; }
634-
635662
void removeEllipsis() {
636-
if (HasEllipsis) {
637-
HasEllipsis = false;
663+
if (hasEllipsis()) {
664+
TupleTypeReprBits.HasEllipsis = false;
638665
getTrailingObjects<SourceLocAndIdx>()[0] = {SourceLoc(), NumElements};
639666
}
640667
}

lib/AST/TypeRepr.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,11 @@ TupleTypeRepr::TupleTypeRepr(ArrayRef<TypeRepr *> Elements, SourceRange Parens,
394394
ArrayRef<SourceLoc> underscoreLocs,
395395
SourceLoc Ellipsis, unsigned EllipsisIdx)
396396
: TypeRepr(TypeReprKind::Tuple), NumElements(Elements.size()),
397-
Parens(Parens), HasEllipsis(Ellipsis.isValid()) {
397+
Parens(Parens) {
398398

399-
NameStatus = ElementNames.empty() ? NotNamed
400-
: underscoreLocs.empty() ? HasNames : HasLabels;
399+
TupleTypeReprBits.NameStatus = ElementNames.empty() ? NotNamed
400+
: underscoreLocs.empty() ? HasNames : HasLabels;
401+
TupleTypeReprBits.HasEllipsis = Ellipsis.isValid();
401402

402403
// Copy elements.
403404
std::uninitialized_copy(Elements.begin(), Elements.end(),

0 commit comments

Comments
 (0)