Skip to content

[AST] NFC: Use llvm::TrailingObjects for TupleType elements #13367

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
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
33 changes: 23 additions & 10 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,13 @@ class alignas(1 << TypeAlignInBits) TypeBase {

/// Whether an element of the tuple is inout.
unsigned HasInOutElement : 1;

unsigned : 32 - (NumTypeBaseBits + 1); // unused / padding

/// The number of elements of the tuple.
unsigned Count : 32;
};
NUMBITS(TupleType, NumTypeBaseBits + 1);
NUMBITS(TupleType, 64);

#undef NUMBITS
union {
Expand Down Expand Up @@ -1577,8 +1582,9 @@ typedef ArrayRefView<TupleTypeElt,CanType,getCanTupleEltType>
/// TupleType - A tuple is a parenthesized list of types where each name has an
/// optional name.
///
class TupleType : public TypeBase, public llvm::FoldingSetNode {
const ArrayRef<TupleTypeElt> Elements;
class TupleType final : public TypeBase, public llvm::FoldingSetNode,
private llvm::TrailingObjects<TupleType, TupleTypeElt> {
friend TrailingObjects;

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

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

unsigned getNumElements() const { return Elements.size(); }
/// getElements - Return the elements of this tuple.
ArrayRef<TupleTypeElt> getElements() const {
return {getTrailingObjects<TupleTypeElt>(), getNumElements()};
}

const TupleTypeElt &getElement(unsigned i) const { return Elements[i]; }
const TupleTypeElt &getElement(unsigned i) const {
return getTrailingObjects<TupleTypeElt>()[i];
}

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

TupleEltTypeArrayRef getElementTypes() const {
Expand Down Expand Up @@ -1631,7 +1641,7 @@ class TupleType : public TypeBase, public llvm::FoldingSetNode {
}

void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, Elements);
Profile(ID, getElements());
}
static void Profile(llvm::FoldingSetNodeID &ID,
ArrayRef<TupleTypeElt> Elements);
Expand All @@ -1640,8 +1650,11 @@ class TupleType : public TypeBase, public llvm::FoldingSetNode {
TupleType(ArrayRef<TupleTypeElt> elements, const ASTContext *CanCtx,
RecursiveTypeProperties properties,
bool hasInOut)
: TypeBase(TypeKind::Tuple, CanCtx, properties), Elements(elements) {
: TypeBase(TypeKind::Tuple, CanCtx, properties) {
TupleTypeBits.HasInOutElement = hasInOut;
TupleTypeBits.Count = elements.size();
std::uninitialized_copy(elements.begin(), elements.end(),
getTrailingObjects<TupleTypeElt>());
}
};
BEGIN_CAN_TYPE_WRAPPER(TupleType, Type)
Expand Down
15 changes: 6 additions & 9 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3077,10 +3077,6 @@ Type TupleType::get(ArrayRef<TupleTypeElt> Fields, const ASTContext &C) {
= C.Impl.getArena(arena).TupleTypes.FindNodeOrInsertPos(ID,InsertPos))
return TT;

// Make a copy of the fields list into ASTContext owned memory.
TupleTypeElt *FieldsCopy =
C.AllocateCopy<TupleTypeElt>(Fields.begin(), Fields.end(), arena);

bool IsCanonical = true; // All canonical elts means this is canonical.
for (const TupleTypeElt &Elt : Fields) {
if (Elt.getType().isNull() || !Elt.getType()->isCanonical()) {
Expand All @@ -3089,11 +3085,12 @@ Type TupleType::get(ArrayRef<TupleTypeElt> Fields, const ASTContext &C) {
}
}

Fields = ArrayRef<TupleTypeElt>(FieldsCopy, Fields.size());

TupleType *New =
new (C, arena) TupleType(Fields, IsCanonical ? &C : nullptr,
properties, hasInOut);
// TupleType will copy the fields list into ASTContext owned memory.
void *mem = C.Allocate(sizeof(TupleType) +
sizeof(TupleTypeElt) * Fields.size(),
alignof(TupleType), arena);
auto New = new (mem) TupleType(Fields, IsCanonical ? &C : nullptr, properties,
hasInOut);
C.Impl.getArena(arena).TupleTypes.InsertNode(New, InsertPos);
return New;
}
Expand Down
16 changes: 8 additions & 8 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2384,8 +2384,8 @@ bool TypeBase::matches(Type other, TypeMatchOptions matchMode,
/// getNamedElementId - If this tuple has a field with the specified name,
/// return the field index, otherwise return -1.
int TupleType::getNamedElementId(Identifier I) const {
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
if (Elements[i].getName() == I)
for (unsigned i = 0, e = TupleTypeBits.Count; i != e; ++i) {
if (getTrailingObjects<TupleTypeElt>()[i].getName() == I)
return i;
}

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

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

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

return Type();
Expand Down