Skip to content

Commit 5590484

Browse files
committed
[AST] NFC: Migrate CompositionTypeRepr to llvm::TrailingObjects
1 parent 00d35ab commit 5590484

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class alignas(8) TypeRepr {
8787
NumComponents : 32
8888
);
8989

90+
SWIFT_INLINE_BITFIELD_FULL(CompositionTypeRepr, TypeRepr, 32,
91+
: NumPadBits,
92+
NumTypes : 32
93+
);
94+
9095
} Bits;
9196

9297
TypeRepr(TypeReprKind K) {
@@ -779,24 +784,30 @@ class TupleTypeRepr final : public TypeRepr,
779784
/// \code
780785
/// Foo & Bar
781786
/// \endcode
782-
class CompositionTypeRepr : public TypeRepr {
783-
ArrayRef<TypeRepr *> Types;
787+
class CompositionTypeRepr final : public TypeRepr,
788+
private llvm::TrailingObjects<CompositionTypeRepr, TypeRepr*> {
789+
friend TrailingObjects;
784790
SourceLoc FirstTypeLoc;
785791
SourceRange CompositionRange;
786792

787-
public:
788793
CompositionTypeRepr(ArrayRef<TypeRepr *> Types,
789794
SourceLoc FirstTypeLoc,
790795
SourceRange CompositionRange)
791-
: TypeRepr(TypeReprKind::Composition), Types(Types),
792-
FirstTypeLoc(FirstTypeLoc), CompositionRange(CompositionRange) {
796+
: TypeRepr(TypeReprKind::Composition), FirstTypeLoc(FirstTypeLoc),
797+
CompositionRange(CompositionRange) {
798+
Bits.CompositionTypeRepr.NumTypes = Types.size();
799+
std::uninitialized_copy(Types.begin(), Types.end(),
800+
getTrailingObjects<TypeRepr*>());
793801
}
794802

795-
ArrayRef<TypeRepr *> getTypes() const { return Types; }
803+
public:
804+
ArrayRef<TypeRepr *> getTypes() const {
805+
return {getTrailingObjects<TypeRepr*>(), Bits.CompositionTypeRepr.NumTypes};
806+
}
796807
SourceLoc getSourceLoc() const { return FirstTypeLoc; }
797808
SourceRange getCompositionRange() const { return CompositionRange; }
798809

799-
static CompositionTypeRepr *create(ASTContext &C,
810+
static CompositionTypeRepr *create(const ASTContext &C,
800811
ArrayRef<TypeRepr*> Protocols,
801812
SourceLoc FirstTypeLoc,
802813
SourceRange CompositionRange);

lib/AST/TypeRepr.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@ TypeRepr *CloneVisitor::visitTupleTypeRepr(TupleTypeRepr *T) {
198198

199199
TypeRepr *CloneVisitor::visitCompositionTypeRepr(CompositionTypeRepr *T) {
200200
// Clone the protocols.
201-
auto types = Ctx.Allocate<TypeRepr*>(T->getTypes().size());
202-
for (unsigned argI : indices(types)) {
203-
types[argI] = cast<TypeRepr>(visit(T->getTypes()[argI]));
201+
SmallVector<TypeRepr*, 8> types;
202+
types.reserve(T->getTypes().size());
203+
for (auto &type : T->getTypes()) {
204+
types.push_back(cast<TypeRepr>(visit(type)));
204205
}
205206

206-
return new (Ctx) CompositionTypeRepr(types,
207-
T->getStartLoc(),
208-
T->getCompositionRange());
207+
return CompositionTypeRepr::create(Ctx, types, T->getStartLoc(),
208+
T->getCompositionRange());
209209
}
210210

211211
TypeRepr *CloneVisitor::visitMetatypeTypeRepr(MetatypeTypeRepr *T) {
@@ -525,21 +525,21 @@ void TupleTypeRepr::printImpl(ASTPrinter &Printer,
525525
Printer << ")";
526526
}
527527

528-
CompositionTypeRepr *
529-
CompositionTypeRepr::create(ASTContext &C,
530-
ArrayRef<TypeRepr *> Types,
531-
SourceLoc FirstTypeLoc,
532-
SourceRange CompositionRange) {
533-
return new (C) CompositionTypeRepr(C.AllocateCopy(Types),
534-
FirstTypeLoc, CompositionRange);
528+
CompositionTypeRepr *CompositionTypeRepr::create(const ASTContext &C,
529+
ArrayRef<TypeRepr *> Types,
530+
SourceLoc FirstTypeLoc,
531+
SourceRange CompositionRange) {
532+
auto size = totalSizeToAlloc<TypeRepr*>(Types.size());
533+
auto mem = C.Allocate(size, alignof(CompositionTypeRepr));
534+
return new (mem) CompositionTypeRepr(Types, FirstTypeLoc, CompositionRange);
535535
}
536536

537537
void CompositionTypeRepr::printImpl(ASTPrinter &Printer,
538538
const PrintOptions &Opts) const {
539-
if (Types.empty()) {
539+
if (getTypes().empty()) {
540540
Printer << "Any";
541541
} else {
542-
interleave(Types, [&](TypeRepr *T) { printTypeRepr(T, Printer, Opts); },
542+
interleave(getTypes(), [&](TypeRepr *T) { printTypeRepr(T, Printer, Opts);},
543543
[&] { Printer << " & "; });
544544
}
545545
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,7 @@ TypeExpr *PreCheckExpression::simplifyTypeExpr(Expr *E) {
14191419
if (!rhs) return nullptr;
14201420
Types.push_back(rhs->getTypeRepr());
14211421

1422-
auto CompRepr = new (TC.Context) CompositionTypeRepr(
1423-
TC.Context.AllocateCopy(Types),
1422+
auto CompRepr = CompositionTypeRepr::create(TC.Context, Types,
14241423
lhsExpr->getStartLoc(), binaryExpr->getSourceRange());
14251424
return new (TC.Context) TypeExpr(TypeLoc(CompRepr, Type()));
14261425
}

0 commit comments

Comments
 (0)