Skip to content

Commit af56e84

Browse files
authored
Merge pull request #13646 from davezarzycki/nfc_more_typerepr_trailing_objects
2 parents d726bd8 + 5590484 commit af56e84

File tree

10 files changed

+160
-88
lines changed

10 files changed

+160
-88
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,30 @@ class alignas(8) TypeRepr {
6666
Warned : 1
6767
);
6868

69-
SWIFT_INLINE_BITFIELD_FULL(TupleTypeRepr, TypeRepr, 1+16,
69+
SWIFT_INLINE_BITFIELD_FULL(TupleTypeRepr, TypeRepr, 1+32,
7070
/// Whether this tuple has '...' and its position.
7171
HasEllipsis : 1,
7272
: NumPadBits,
7373
/// The number of elements contained.
74-
NumElements : 16
74+
NumElements : 32
75+
);
76+
77+
SWIFT_INLINE_BITFIELD_EMPTY(IdentTypeRepr, TypeRepr);
78+
SWIFT_INLINE_BITFIELD_EMPTY(ComponentIdentTypeRepr, IdentTypeRepr);
79+
80+
SWIFT_INLINE_BITFIELD_FULL(GenericIdentTypeRepr, ComponentIdentTypeRepr, 32,
81+
: NumPadBits,
82+
NumGenericArgs : 32
83+
);
84+
85+
SWIFT_INLINE_BITFIELD_FULL(CompoundIdentTypeRepr, IdentTypeRepr, 32,
86+
: NumPadBits,
87+
NumComponents : 32
88+
);
89+
90+
SWIFT_INLINE_BITFIELD_FULL(CompositionTypeRepr, TypeRepr, 32,
91+
: NumPadBits,
92+
NumTypes : 32
7593
);
7694

7795
} Bits;
@@ -319,24 +337,37 @@ class SimpleIdentTypeRepr : public ComponentIdentTypeRepr {
319337
/// \code
320338
/// Bar<Gen>
321339
/// \endcode
322-
class GenericIdentTypeRepr : public ComponentIdentTypeRepr {
323-
ArrayRef<TypeRepr*> GenericArgs;
340+
class GenericIdentTypeRepr final : public ComponentIdentTypeRepr,
341+
private llvm::TrailingObjects<GenericIdentTypeRepr, TypeRepr *> {
342+
friend TrailingObjects;
324343
SourceRange AngleBrackets;
325344

326-
public:
327345
GenericIdentTypeRepr(SourceLoc Loc, Identifier Id,
328346
ArrayRef<TypeRepr*> GenericArgs,
329347
SourceRange AngleBrackets)
330348
: ComponentIdentTypeRepr(TypeReprKind::GenericIdent, Loc, Id),
331-
GenericArgs(GenericArgs), AngleBrackets(AngleBrackets) {
349+
AngleBrackets(AngleBrackets) {
350+
Bits.GenericIdentTypeRepr.NumGenericArgs = GenericArgs.size();
332351
assert(!GenericArgs.empty());
333352
#ifndef NDEBUG
334353
for (auto arg : GenericArgs)
335354
assert(arg != nullptr);
336355
#endif
356+
std::uninitialized_copy(GenericArgs.begin(), GenericArgs.end(),
357+
getTrailingObjects<TypeRepr*>());
337358
}
338359

339-
ArrayRef<TypeRepr*> getGenericArgs() const { return GenericArgs; }
360+
public:
361+
static GenericIdentTypeRepr *create(const ASTContext &C,
362+
SourceLoc Loc,
363+
Identifier Id,
364+
ArrayRef<TypeRepr*> GenericArgs,
365+
SourceRange AngleBrackets);
366+
367+
ArrayRef<TypeRepr*> getGenericArgs() const {
368+
return {getTrailingObjects<TypeRepr*>(),
369+
Bits.GenericIdentTypeRepr.NumGenericArgs};
370+
}
340371
SourceRange getAngleBrackets() const { return AngleBrackets; }
341372

342373
static bool classof(const TypeRepr *T) {
@@ -354,15 +385,27 @@ class GenericIdentTypeRepr : public ComponentIdentTypeRepr {
354385
/// \code
355386
/// Foo.Bar<Gen>
356387
/// \endcode
357-
class CompoundIdentTypeRepr : public IdentTypeRepr {
358-
public:
359-
const ArrayRef<ComponentIdentTypeRepr *> Components;
388+
class CompoundIdentTypeRepr final : public IdentTypeRepr,
389+
private llvm::TrailingObjects<CompoundIdentTypeRepr,
390+
ComponentIdentTypeRepr *> {
391+
friend TrailingObjects;
360392

361-
explicit CompoundIdentTypeRepr(ArrayRef<ComponentIdentTypeRepr *> Components)
362-
: IdentTypeRepr(TypeReprKind::CompoundIdent),
363-
Components(Components) {
393+
CompoundIdentTypeRepr(ArrayRef<ComponentIdentTypeRepr *> Components)
394+
: IdentTypeRepr(TypeReprKind::CompoundIdent) {
395+
Bits.CompoundIdentTypeRepr.NumComponents = Components.size();
364396
assert(Components.size() > 1 &&
365397
"should have just used the single ComponentIdentTypeRepr directly");
398+
std::uninitialized_copy(Components.begin(), Components.end(),
399+
getTrailingObjects<ComponentIdentTypeRepr*>());
400+
}
401+
402+
public:
403+
static CompoundIdentTypeRepr *create(const ASTContext &Ctx,
404+
ArrayRef<ComponentIdentTypeRepr*> Components);
405+
406+
ArrayRef<ComponentIdentTypeRepr*> getComponents() const {
407+
return {getTrailingObjects<ComponentIdentTypeRepr*>(),
408+
Bits.CompoundIdentTypeRepr.NumComponents};
366409
}
367410

368411
static bool classof(const TypeRepr *T) {
@@ -371,9 +414,15 @@ class CompoundIdentTypeRepr : public IdentTypeRepr {
371414
static bool classof(const CompoundIdentTypeRepr *T) { return true; }
372415

373416
private:
374-
SourceLoc getStartLocImpl() const { return Components.front()->getStartLoc();}
375-
SourceLoc getEndLocImpl() const { return Components.back()->getEndLoc(); }
376-
SourceLoc getLocImpl() const { return Components.back()->getLoc(); }
417+
SourceLoc getStartLocImpl() const {
418+
return getComponents().front()->getStartLoc();
419+
}
420+
SourceLoc getEndLocImpl() const {
421+
return getComponents().back()->getEndLoc();
422+
}
423+
SourceLoc getLocImpl() const {
424+
return getComponents().back()->getLoc();
425+
}
377426

378427
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
379428
friend class TypeRepr;
@@ -392,13 +441,13 @@ class IdentTypeRepr::ComponentRange {
392441
iterator begin() const {
393442
if (isa<ComponentIdentTypeRepr>(IdT))
394443
return reinterpret_cast<iterator>(&IdT);
395-
return cast<CompoundIdentTypeRepr>(IdT)->Components.begin();
444+
return cast<CompoundIdentTypeRepr>(IdT)->getComponents().begin();
396445
}
397446

398447
iterator end() const {
399448
if (isa<ComponentIdentTypeRepr>(IdT))
400449
return reinterpret_cast<iterator>(&IdT) + 1;
401-
return cast<CompoundIdentTypeRepr>(IdT)->Components.end();
450+
return cast<CompoundIdentTypeRepr>(IdT)->getComponents().end();
402451
}
403452

404453
bool empty() const { return begin() == end(); }
@@ -735,24 +784,30 @@ class TupleTypeRepr final : public TypeRepr,
735784
/// \code
736785
/// Foo & Bar
737786
/// \endcode
738-
class CompositionTypeRepr : public TypeRepr {
739-
ArrayRef<TypeRepr *> Types;
787+
class CompositionTypeRepr final : public TypeRepr,
788+
private llvm::TrailingObjects<CompositionTypeRepr, TypeRepr*> {
789+
friend TrailingObjects;
740790
SourceLoc FirstTypeLoc;
741791
SourceRange CompositionRange;
742792

743-
public:
744793
CompositionTypeRepr(ArrayRef<TypeRepr *> Types,
745794
SourceLoc FirstTypeLoc,
746795
SourceRange CompositionRange)
747-
: TypeRepr(TypeReprKind::Composition), Types(Types),
748-
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*>());
749801
}
750802

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

755-
static CompositionTypeRepr *create(ASTContext &C,
810+
static CompositionTypeRepr *create(const ASTContext &C,
756811
ArrayRef<TypeRepr*> Protocols,
757812
SourceLoc FirstTypeLoc,
758813
SourceRange CompositionRange);

include/swift/AST/TypeReprNodes.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ TYPEREPR(Attributed, TypeRepr)
4040

4141
ABSTRACT_TYPEREPR(Ident, TypeRepr)
4242
ABSTRACT_TYPEREPR(ComponentIdent, IdentTypeRepr)
43-
TYPEREPR(SimpleIdent, ComponentIdentTypeRepr)
44-
TYPEREPR(GenericIdent, ComponentIdentTypeRepr)
43+
TYPEREPR(SimpleIdent, ComponentIdentTypeRepr)
44+
TYPEREPR(GenericIdent, ComponentIdentTypeRepr)
4545
TYPEREPR(CompoundIdent, IdentTypeRepr)
4646

4747
TYPEREPR(Function, TypeRepr)

lib/AST/ASTWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ bool Traversal::visitGenericIdentTypeRepr(GenericIdentTypeRepr *T) {
16341634
}
16351635

16361636
bool Traversal::visitCompoundIdentTypeRepr(CompoundIdentTypeRepr *T) {
1637-
for (auto comp : T->Components) {
1637+
for (auto comp : T->getComponents()) {
16381638
if (doIt(comp))
16391639
return true;
16401640
}

lib/AST/Expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,7 @@ TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
21112111
}
21122112
}
21132113

2114-
auto *genericComp = new (C) GenericIdentTypeRepr(
2114+
auto *genericComp = GenericIdentTypeRepr::create(C,
21152115
last->getIdLoc(), last->getIdentifier(),
21162116
Args, AngleLocs);
21172117
genericComp->setValue(last->getBoundDecl(), last->getDeclContext());

lib/AST/TypeRepr.cpp

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,23 @@ TypeRepr *CloneVisitor::visitSimpleIdentTypeRepr(SimpleIdentTypeRepr *T) {
137137

138138
TypeRepr *CloneVisitor::visitGenericIdentTypeRepr(GenericIdentTypeRepr *T) {
139139
// Clone the generic arguments.
140-
auto genericArgs = Ctx.Allocate<TypeRepr*>(T->getGenericArgs().size());
141-
for (unsigned argI : indices(genericArgs)) {
142-
genericArgs[argI] = visit(T->getGenericArgs()[argI]);
140+
SmallVector<TypeRepr*, 8> genericArgs;
141+
genericArgs.reserve(T->getGenericArgs().size());
142+
for (auto &arg : T->getGenericArgs()) {
143+
genericArgs.push_back(visit(arg));
143144
}
144-
return new (Ctx) GenericIdentTypeRepr(T->getIdLoc(), T->getIdentifier(),
145+
return GenericIdentTypeRepr::create(Ctx, T->getIdLoc(), T->getIdentifier(),
145146
genericArgs, T->getAngleBrackets());
146147
}
147148

148149
TypeRepr *CloneVisitor::visitCompoundIdentTypeRepr(CompoundIdentTypeRepr *T) {
149150
// Clone the components.
150-
auto components = Ctx.Allocate<ComponentIdentTypeRepr*>(T->Components.size());
151-
for (unsigned I : indices(components)) {
152-
components[I] = cast<ComponentIdentTypeRepr>(visit(T->Components[I]));
151+
SmallVector<ComponentIdentTypeRepr*, 8> components;
152+
components.reserve(T->getComponents().size());
153+
for (auto &component : T->getComponents()) {
154+
components.push_back(cast<ComponentIdentTypeRepr>(visit(component)));
153155
}
154-
return new (Ctx) CompoundIdentTypeRepr(components);
156+
return CompoundIdentTypeRepr::create(Ctx, components);
155157
}
156158

157159
TypeRepr *CloneVisitor::visitFunctionTypeRepr(FunctionTypeRepr *T) {
@@ -196,14 +198,14 @@ TypeRepr *CloneVisitor::visitTupleTypeRepr(TupleTypeRepr *T) {
196198

197199
TypeRepr *CloneVisitor::visitCompositionTypeRepr(CompositionTypeRepr *T) {
198200
// Clone the protocols.
199-
auto types = Ctx.Allocate<TypeRepr*>(T->getTypes().size());
200-
for (unsigned argI : indices(types)) {
201-
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)));
202205
}
203206

204-
return new (Ctx) CompositionTypeRepr(types,
205-
T->getStartLoc(),
206-
T->getCompositionRange());
207+
return CompositionTypeRepr::create(Ctx, types, T->getStartLoc(),
208+
T->getCompositionRange());
207209
}
208210

209211
TypeRepr *CloneVisitor::visitMetatypeTypeRepr(MetatypeTypeRepr *T) {
@@ -325,7 +327,7 @@ IdentTypeRepr *IdentTypeRepr::create(ASTContext &C,
325327
if (Components.size() == 1)
326328
return Components.front();
327329

328-
return new (C) CompoundIdentTypeRepr(C.AllocateCopy(Components));
330+
return CompoundIdentTypeRepr::create(C, Components);
329331
}
330332

331333
static void printGenericArgs(ASTPrinter &Printer, const PrintOptions &Opts,
@@ -356,8 +358,8 @@ void ComponentIdentTypeRepr::printImpl(ASTPrinter &Printer,
356358

357359
void CompoundIdentTypeRepr::printImpl(ASTPrinter &Printer,
358360
const PrintOptions &Opts) const {
359-
printTypeRepr(Components.front(), Printer, Opts);
360-
for (auto C : Components.slice(1)) {
361+
printTypeRepr(getComponents().front(), Printer, Opts);
362+
for (auto C : getComponents().slice(1)) {
361363
Printer << ".";
362364
printTypeRepr(C, Printer, Opts);
363365
}
@@ -410,12 +412,12 @@ TupleTypeRepr::TupleTypeRepr(ArrayRef<TupleTypeReprElement> Elements,
410412
SourceRange Parens,
411413
SourceLoc Ellipsis, unsigned EllipsisIdx)
412414
: TypeRepr(TypeReprKind::Tuple), Parens(Parens) {
415+
Bits.TupleTypeRepr.HasEllipsis = Ellipsis.isValid();
416+
Bits.TupleTypeRepr.NumElements = Elements.size();
413417

414418
// Copy elements.
415419
std::uninitialized_copy(Elements.begin(), Elements.end(),
416420
getTrailingObjects<TupleTypeReprElement>());
417-
Bits.TupleTypeRepr.HasEllipsis = Ellipsis.isValid();
418-
Bits.TupleTypeRepr.NumElements = Elements.size();
419421

420422
// Set ellipsis location and index.
421423
if (Ellipsis.isValid()) {
@@ -444,6 +446,23 @@ TupleTypeRepr *TupleTypeRepr::createEmpty(const ASTContext &C,
444446
/*Ellipsis=*/SourceLoc(), /*EllipsisIdx=*/0);
445447
}
446448

449+
GenericIdentTypeRepr *GenericIdentTypeRepr::create(const ASTContext &C,
450+
SourceLoc Loc,
451+
Identifier Id,
452+
ArrayRef<TypeRepr*> GenericArgs,
453+
SourceRange AngleBrackets) {
454+
auto size = totalSizeToAlloc<TypeRepr*>(GenericArgs.size());
455+
auto mem = C.Allocate(size, alignof(GenericIdentTypeRepr));
456+
return new (mem) GenericIdentTypeRepr(Loc, Id, GenericArgs, AngleBrackets);
457+
}
458+
459+
CompoundIdentTypeRepr *CompoundIdentTypeRepr::create(const ASTContext &C,
460+
ArrayRef<ComponentIdentTypeRepr*> Components) {
461+
auto size = totalSizeToAlloc<ComponentIdentTypeRepr*>(Components.size());
462+
auto mem = C.Allocate(size, alignof(CompoundIdentTypeRepr));
463+
return new (mem) CompoundIdentTypeRepr(Components);
464+
}
465+
447466
SILBoxTypeRepr *SILBoxTypeRepr::create(ASTContext &C,
448467
GenericParamList *GenericParams,
449468
SourceLoc LBraceLoc, ArrayRef<Field> Fields,
@@ -506,21 +525,21 @@ void TupleTypeRepr::printImpl(ASTPrinter &Printer,
506525
Printer << ")";
507526
}
508527

509-
CompositionTypeRepr *
510-
CompositionTypeRepr::create(ASTContext &C,
511-
ArrayRef<TypeRepr *> Types,
512-
SourceLoc FirstTypeLoc,
513-
SourceRange CompositionRange) {
514-
return new (C) CompositionTypeRepr(C.AllocateCopy(Types),
515-
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);
516535
}
517536

518537
void CompositionTypeRepr::printImpl(ASTPrinter &Printer,
519538
const PrintOptions &Opts) const {
520-
if (Types.empty()) {
539+
if (getTypes().empty()) {
521540
Printer << "Any";
522541
} else {
523-
interleave(Types, [&](TypeRepr *T) { printTypeRepr(T, Printer, Opts); },
542+
interleave(getTypes(), [&](TypeRepr *T) { printTypeRepr(T, Printer, Opts);},
524543
[&] { Printer << " & "; });
525544
}
526545
}

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
199199
}
200200

201201
FoundResult visitCompoundIdentTypeRepr(CompoundIdentTypeRepr *T) {
202-
return visit(T->Components.back());
202+
return visit(T->getComponents().back());
203203
}
204204

205205
FoundResult visitOptionalTypeRepr(OptionalTypeRepr *T) {

lib/Parse/ParseType.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,7 @@ ParserResult<TypeRepr> Parser::parseTypeIdentifier() {
595595

596596
ComponentIdentTypeRepr *CompT;
597597
if (!GenericArgs.empty())
598-
CompT = new (Context) GenericIdentTypeRepr(Loc, Name,
599-
Context.AllocateCopy(GenericArgs),
598+
CompT = GenericIdentTypeRepr::create(Context, Loc, Name, GenericArgs,
600599
SourceRange(LAngle, RAngle));
601600
else
602601
CompT = new (Context) SimpleIdentTypeRepr(Loc, Name);

0 commit comments

Comments
 (0)