Skip to content

Commit f45b165

Browse files
committed
[AST] NFC: Use llvm::TrailingObjects for TupleType elements
1 parent b77b541 commit f45b165

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

include/swift/AST/Types.h

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,13 @@ class alignas(1 << TypeAlignInBits) TypeBase {
362362

363363
/// Whether an element of the tuple is inout.
364364
unsigned HasInOutElement : 1;
365+
366+
unsigned : 32 - (NumTypeBaseBits + 1); // unused / padding
367+
368+
/// The number of elements of the tuple.
369+
unsigned Count : 32;
365370
};
366-
NUMBITS(TupleType, NumTypeBaseBits + 1);
371+
NUMBITS(TupleType, 64);
367372

368373
#undef NUMBITS
369374
union {
@@ -1577,8 +1582,9 @@ typedef ArrayRefView<TupleTypeElt,CanType,getCanTupleEltType>
15771582
/// TupleType - A tuple is a parenthesized list of types where each name has an
15781583
/// optional name.
15791584
///
1580-
class TupleType : public TypeBase, public llvm::FoldingSetNode {
1581-
const ArrayRef<TupleTypeElt> Elements;
1585+
class TupleType final : public TypeBase, public llvm::FoldingSetNode,
1586+
private llvm::TrailingObjects<TupleType, TupleTypeElt> {
1587+
friend TrailingObjects;
15821588

15831589
public:
15841590
/// get - Return the uniqued tuple type with the specified elements.
@@ -1590,16 +1596,20 @@ class TupleType : public TypeBase, public llvm::FoldingSetNode {
15901596
/// getEmpty - Return the empty tuple type '()'.
15911597
static CanTypeWrapper<TupleType> getEmpty(const ASTContext &C);
15921598

1593-
/// getFields - Return the fields of this tuple.
1594-
ArrayRef<TupleTypeElt> getElements() const { return Elements; }
1599+
unsigned getNumElements() const { return TupleTypeBits.Count; }
15951600

1596-
unsigned getNumElements() const { return Elements.size(); }
1601+
/// getElements - Return the elements of this tuple.
1602+
ArrayRef<TupleTypeElt> getElements() const {
1603+
return {getTrailingObjects<TupleTypeElt>(), getNumElements()};
1604+
}
15971605

1598-
const TupleTypeElt &getElement(unsigned i) const { return Elements[i]; }
1606+
const TupleTypeElt &getElement(unsigned i) const {
1607+
return getTrailingObjects<TupleTypeElt>()[i];
1608+
}
15991609

16001610
/// getElementType - Return the type of the specified element.
16011611
Type getElementType(unsigned ElementNo) const {
1602-
return Elements[ElementNo].getType();
1612+
return getTrailingObjects<TupleTypeElt>()[ElementNo].getType();
16031613
}
16041614

16051615
TupleEltTypeArrayRef getElementTypes() const {
@@ -1631,7 +1641,7 @@ class TupleType : public TypeBase, public llvm::FoldingSetNode {
16311641
}
16321642

16331643
void Profile(llvm::FoldingSetNodeID &ID) {
1634-
Profile(ID, Elements);
1644+
Profile(ID, getElements());
16351645
}
16361646
static void Profile(llvm::FoldingSetNodeID &ID,
16371647
ArrayRef<TupleTypeElt> Elements);
@@ -1640,8 +1650,11 @@ class TupleType : public TypeBase, public llvm::FoldingSetNode {
16401650
TupleType(ArrayRef<TupleTypeElt> elements, const ASTContext *CanCtx,
16411651
RecursiveTypeProperties properties,
16421652
bool hasInOut)
1643-
: TypeBase(TypeKind::Tuple, CanCtx, properties), Elements(elements) {
1653+
: TypeBase(TypeKind::Tuple, CanCtx, properties) {
16441654
TupleTypeBits.HasInOutElement = hasInOut;
1655+
TupleTypeBits.Count = elements.size();
1656+
std::uninitialized_copy(elements.begin(), elements.end(),
1657+
getTrailingObjects<TupleTypeElt>());
16451658
}
16461659
};
16471660
BEGIN_CAN_TYPE_WRAPPER(TupleType, Type)

lib/AST/ASTContext.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3077,10 +3077,6 @@ Type TupleType::get(ArrayRef<TupleTypeElt> Fields, const ASTContext &C) {
30773077
= C.Impl.getArena(arena).TupleTypes.FindNodeOrInsertPos(ID,InsertPos))
30783078
return TT;
30793079

3080-
// Make a copy of the fields list into ASTContext owned memory.
3081-
TupleTypeElt *FieldsCopy =
3082-
C.AllocateCopy<TupleTypeElt>(Fields.begin(), Fields.end(), arena);
3083-
30843080
bool IsCanonical = true; // All canonical elts means this is canonical.
30853081
for (const TupleTypeElt &Elt : Fields) {
30863082
if (Elt.getType().isNull() || !Elt.getType()->isCanonical()) {
@@ -3089,11 +3085,12 @@ Type TupleType::get(ArrayRef<TupleTypeElt> Fields, const ASTContext &C) {
30893085
}
30903086
}
30913087

3092-
Fields = ArrayRef<TupleTypeElt>(FieldsCopy, Fields.size());
3093-
3094-
TupleType *New =
3095-
new (C, arena) TupleType(Fields, IsCanonical ? &C : nullptr,
3096-
properties, hasInOut);
3088+
// TupleType will copy the fields list into ASTContext owned memory.
3089+
void *mem = C.Allocate(sizeof(TupleType) +
3090+
sizeof(TupleTypeElt) * Fields.size(),
3091+
alignof(TupleType), arena);
3092+
auto New = new (mem) TupleType(Fields, IsCanonical ? &C : nullptr, properties,
3093+
hasInOut);
30973094
C.Impl.getArena(arena).TupleTypes.InsertNode(New, InsertPos);
30983095
return New;
30993096
}

lib/AST/Type.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,8 +2384,8 @@ bool TypeBase::matches(Type other, TypeMatchOptions matchMode,
23842384
/// getNamedElementId - If this tuple has a field with the specified name,
23852385
/// return the field index, otherwise return -1.
23862386
int TupleType::getNamedElementId(Identifier I) const {
2387-
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
2388-
if (Elements[i].getName() == I)
2387+
for (unsigned i = 0, e = TupleTypeBits.Count; i != e; ++i) {
2388+
if (getTrailingObjects<TupleTypeElt>()[i].getName() == I)
23892389
return i;
23902390
}
23912391

@@ -2397,15 +2397,15 @@ int TupleType::getNamedElementId(Identifier I) const {
23972397
/// scalar, return the field number that the scalar is assigned to. If not,
23982398
/// return -1.
23992399
int TupleType::getElementForScalarInit() const {
2400-
if (Elements.empty()) return -1;
2400+
if (TupleTypeBits.Count == 0) return -1;
24012401

24022402
int FieldWithoutDefault = -1;
2403-
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
2403+
for (unsigned i = 0, e = TupleTypeBits.Count; i != e; ++i) {
24042404
// If we already saw a non-vararg field missing a default value, then we
24052405
// cannot assign a scalar to this tuple.
24062406
if (FieldWithoutDefault != -1) {
24072407
// Vararg fields are okay; they'll just end up being empty.
2408-
if (Elements[i].isVararg())
2408+
if (getTrailingObjects<TupleTypeElt>()[i].isVararg())
24092409
continue;
24102410

24112411
return -1;
@@ -2424,9 +2424,9 @@ int TupleType::getElementForScalarInit() const {
24242424
/// varargs element (i.e., if it is "Int...", this returns Int, not [Int]).
24252425
/// Otherwise, this returns Type().
24262426
Type TupleType::getVarArgsBaseType() const {
2427-
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
2428-
if (Elements[i].isVararg())
2429-
return Elements[i].getVarargBaseTy();
2427+
for (unsigned i = 0, e = TupleTypeBits.Count; i != e; ++i) {
2428+
if (getTrailingObjects<TupleTypeElt>()[i].isVararg())
2429+
return getTrailingObjects<TupleTypeElt>()[i].getVarargBaseTy();
24302430
}
24312431

24322432
return Type();

0 commit comments

Comments
 (0)