Skip to content

Commit 0281b23

Browse files
committed
[AST] Tail allocate ellipsis location and index in TupleTypeRepr
1 parent c02609e commit 0281b23

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

include/swift/AST/TypeRepr.h

+18-10
Original file line numberDiff line numberDiff line change
@@ -545,20 +545,20 @@ class ImplicitlyUnwrappedOptionalTypeRepr : public TypeRepr {
545545
/// \endcode
546546
class TupleTypeRepr final : public TypeRepr,
547547
private llvm::TrailingObjects<TupleTypeRepr, TypeRepr*, Identifier,
548-
SourceLoc> {
548+
SourceLoc, std::pair<SourceLoc, unsigned>> {
549549
friend TrailingObjects;
550+
typedef std::pair<SourceLoc, unsigned> SourceLocAndIdx;
550551

551552
unsigned NumElements;
552553
SourceRange Parens;
553-
SourceLoc Ellipsis;
554-
unsigned EllipsisIdx;
555554

556555
enum {
557556
NotNamed = 0,
558557
HasNames = 1,
559558
HasLabels = 2
560-
} NameStatus;
561-
559+
} NameStatus : 2;
560+
bool HasEllipsis : 1;
561+
562562
size_t numTrailingObjects(OverloadToken<TypeRepr *>) const {
563563
return NumElements;
564564
}
@@ -622,13 +622,21 @@ class TupleTypeRepr final : public TypeRepr,
622622
}
623623

624624
SourceRange getParens() const { return Parens; }
625-
SourceLoc getEllipsisLoc() const { return Ellipsis; }
626-
unsigned getEllipsisIndex() const { return EllipsisIdx; }
627-
bool hasEllipsis() const { return Ellipsis.isValid(); }
625+
SourceLoc getEllipsisLoc() const {
626+
return HasEllipsis ?
627+
getTrailingObjects<SourceLocAndIdx>()[0].first : SourceLoc();
628+
}
629+
unsigned getEllipsisIndex() const {
630+
return HasEllipsis ?
631+
getTrailingObjects<SourceLocAndIdx>()[0].second : NumElements;
632+
}
633+
bool hasEllipsis() const { return HasEllipsis; }
628634

629635
void removeEllipsis() {
630-
Ellipsis = SourceLoc();
631-
EllipsisIdx = NumElements;
636+
if (HasEllipsis) {
637+
HasEllipsis = false;
638+
getTrailingObjects<SourceLocAndIdx>()[0] = {SourceLoc(), NumElements};
639+
}
632640
}
633641

634642
bool isParenType() const {

lib/AST/TypeRepr.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ 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), Ellipsis(Ellipsis), EllipsisIdx(EllipsisIdx) {
397+
Parens(Parens), HasEllipsis(Ellipsis.isValid()) {
398398

399399
NameStatus = ElementNames.empty() ? NotNamed
400400
: underscoreLocs.empty() ? HasNames : HasLabels;
@@ -415,6 +415,10 @@ TupleTypeRepr::TupleTypeRepr(ArrayRef<TypeRepr *> Elements, SourceRange Parens,
415415
std::uninitialized_copy(underscoreLocs.begin(), underscoreLocs.end(),
416416
getTrailingObjects<SourceLoc>() +
417417
ElementNameLocs.size());
418+
419+
// Set ellipsis location and index.
420+
if (Ellipsis.isValid())
421+
getTrailingObjects<SourceLocAndIdx>()[0] = {Ellipsis, EllipsisIdx};
418422
}
419423

420424
TupleTypeRepr *TupleTypeRepr::create(const ASTContext &C,
@@ -432,10 +436,12 @@ TupleTypeRepr *TupleTypeRepr::create(const ASTContext &C,
432436
assert(Ellipsis.isValid() ? EllipsisIdx < Elements.size()
433437
: EllipsisIdx == Elements.size());
434438

435-
size_t size = totalSizeToAlloc<TypeRepr *, Identifier, SourceLoc>(
439+
size_t size =
440+
totalSizeToAlloc<TypeRepr *, Identifier, SourceLoc, SourceLocAndIdx>(
436441
Elements.size(),
437442
ElementNames.size(),
438-
ElementNameLocs.size() + underscoreLocs.size());
443+
ElementNameLocs.size() + underscoreLocs.size(),
444+
Ellipsis.isValid() ? 1 : 0);
439445
void *mem = C.Allocate(size, alignof(TupleTypeRepr));
440446
return new (mem) TupleTypeRepr(Elements, Parens, ElementNames,
441447
ElementNameLocs, underscoreLocs,

0 commit comments

Comments
 (0)