Skip to content

[AST] Pack bitfields in TypeRepr #5981

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 1 commit into from
Dec 1, 2016
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
87 changes: 57 additions & 30 deletions include/swift/AST/TypeRepr.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,60 @@ class alignas(8) TypeRepr {
TypeRepr(const TypeRepr&) = delete;
void operator=(const TypeRepr&) = delete;

/// \brief The subclass of TypeRepr that this is.
unsigned Kind : 6;
class TypeReprBitfields {
friend class TypeRepr;
/// The subclass of TypeRepr that this is.
unsigned Kind : 6;

/// Whether this type representation is known to contain an invalid
/// type.
unsigned Invalid : 1;

/// Whether this type representation had a warning emitted related to it.
/// This is a hack related to how we resolve type exprs multiple times in
/// generic contexts.
unsigned Warned : 1;
};
enum { NumTypeReprBits = 8 };
class TupleTypeReprBitfields {
friend class TupleTypeRepr;
unsigned : NumTypeReprBits;
// HasNames, HasLabels?
unsigned NameStatus : 2;
// Whether this tuple has '...' and its position.
unsigned HasEllipsis : 1;
};

/// Whether this type representation is known to contain an invalid
/// type.
unsigned Invalid : 1;
protected:
union {
TypeReprBitfields TypeReprBits;
TupleTypeReprBitfields TupleTypeReprBits;
};

/// Whether this type representation had a warning emitted related to it.
/// This is a hack related to how we resolve type exprs multiple times in
/// generic contexts.
unsigned Warned : 1;
TypeRepr(TypeReprKind K) {
TypeReprBits.Kind = static_cast<unsigned>(K);
TypeReprBits.Invalid = false;
TypeReprBits.Warned = false;
}

private:
SourceLoc getLocImpl() const { return getStartLoc(); }

protected:
TypeRepr(TypeReprKind K)
: Kind(static_cast<unsigned>(K)), Invalid(false), Warned(false) {}

public:
TypeReprKind getKind() const { return static_cast<TypeReprKind>(Kind); }
TypeReprKind getKind() const {
return static_cast<TypeReprKind>(TypeReprBits.Kind);
}

/// Is this type representation known to be invalid?
bool isInvalid() const { return Invalid; }
bool isInvalid() const { return TypeReprBits.Invalid; }

/// Note that this type representation describes an invalid type.
void setInvalid() { Invalid = true; }
void setInvalid() { TypeReprBits.Invalid = true; }

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

/// Get the representative location for pointing at this type.
SourceLoc getLoc() const;
Expand Down Expand Up @@ -556,21 +579,21 @@ class TupleTypeRepr final : public TypeRepr,
NotNamed = 0,
HasNames = 1,
HasLabels = 2
} NameStatus : 2;
bool HasEllipsis : 1;
};

size_t numTrailingObjects(OverloadToken<TypeRepr *>) const {
return NumElements;
}
size_t numTrailingObjects(OverloadToken<Identifier>) const {
return NameStatus >= HasNames ? NumElements : 0;
return TupleTypeReprBits.NameStatus >= HasNames ? NumElements : 0;
}
size_t numTrailingObjects(OverloadToken<SourceLoc>) const {
switch (NameStatus) {
switch (TupleTypeReprBits.NameStatus) {
case NotNamed: return 0;
case HasNames: return NumElements;
case HasLabels: return NumElements + NumElements;
}
llvm_unreachable("all cases should have been handled");
}

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

unsigned getNumElements() const { return NumElements; }
bool hasElementNames() const { return NameStatus >= HasNames; }
bool hasUnderscoreLocs() const { return NameStatus == HasLabels; }
bool hasElementNames() const {
return TupleTypeReprBits.NameStatus >= HasNames;
}
bool hasUnderscoreLocs() const {
return TupleTypeReprBits.NameStatus == HasLabels;
}

ArrayRef<TypeRepr *> getElements() const {
return { getTrailingObjects<TypeRepr *>(), NumElements };
Expand Down Expand Up @@ -622,19 +649,19 @@ class TupleTypeRepr final : public TypeRepr,
}

SourceRange getParens() const { return Parens; }

bool hasEllipsis() const { return TupleTypeReprBits.HasEllipsis; }
SourceLoc getEllipsisLoc() const {
return HasEllipsis ?
return hasEllipsis() ?
getTrailingObjects<SourceLocAndIdx>()[0].first : SourceLoc();
}
unsigned getEllipsisIndex() const {
return HasEllipsis ?
return hasEllipsis() ?
getTrailingObjects<SourceLocAndIdx>()[0].second : NumElements;
}
bool hasEllipsis() const { return HasEllipsis; }

void removeEllipsis() {
if (HasEllipsis) {
HasEllipsis = false;
if (hasEllipsis()) {
TupleTypeReprBits.HasEllipsis = false;
getTrailingObjects<SourceLocAndIdx>()[0] = {SourceLoc(), NumElements};
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/AST/TypeRepr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,11 @@ TupleTypeRepr::TupleTypeRepr(ArrayRef<TypeRepr *> Elements, SourceRange Parens,
ArrayRef<SourceLoc> underscoreLocs,
SourceLoc Ellipsis, unsigned EllipsisIdx)
: TypeRepr(TypeReprKind::Tuple), NumElements(Elements.size()),
Parens(Parens), HasEllipsis(Ellipsis.isValid()) {
Parens(Parens) {

NameStatus = ElementNames.empty() ? NotNamed
: underscoreLocs.empty() ? HasNames : HasLabels;
TupleTypeReprBits.NameStatus = ElementNames.empty() ? NotNamed
: underscoreLocs.empty() ? HasNames : HasLabels;
TupleTypeReprBits.HasEllipsis = Ellipsis.isValid();

// Copy elements.
std::uninitialized_copy(Elements.begin(), Elements.end(),
Expand Down