Skip to content

Commit ac996b9

Browse files
committed
[NFC] CompoundIdentTypeRepr: Supplant getComponents with getBaseComponent & getMemberComponents
Also store the base component separately as an arbitrary `TypeRepr`.
1 parent 5397dc7 commit ac996b9

File tree

7 files changed

+93
-85
lines changed

7 files changed

+93
-85
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
8989

9090
SWIFT_INLINE_BITFIELD_FULL(CompoundIdentTypeRepr, IdentTypeRepr, 32,
9191
: NumPadBits,
92-
NumComponents : 32
92+
NumMemberComponents : 32
9393
);
9494

9595
SWIFT_INLINE_BITFIELD_FULL(CompositionTypeRepr, TypeRepr, 32,
@@ -413,28 +413,29 @@ class CompoundIdentTypeRepr final : public IdentTypeRepr,
413413
ComponentIdentTypeRepr *> {
414414
friend TrailingObjects;
415415

416-
CompoundIdentTypeRepr(ArrayRef<ComponentIdentTypeRepr *> Components)
417-
: IdentTypeRepr(TypeReprKind::CompoundIdent) {
418-
Bits.CompoundIdentTypeRepr.NumComponents = Components.size();
419-
assert(Components.size() > 1 &&
416+
/// The base component, which is not necessarily an identifier type.
417+
TypeRepr *Base;
418+
419+
CompoundIdentTypeRepr(TypeRepr *Base,
420+
ArrayRef<ComponentIdentTypeRepr *> MemberComponents)
421+
: IdentTypeRepr(TypeReprKind::CompoundIdent), Base(Base) {
422+
Bits.CompoundIdentTypeRepr.NumMemberComponents = MemberComponents.size();
423+
assert(MemberComponents.size() > 0 &&
420424
"should have just used the single ComponentIdentTypeRepr directly");
421-
std::uninitialized_copy(Components.begin(), Components.end(),
422-
getTrailingObjects<ComponentIdentTypeRepr*>());
425+
std::uninitialized_copy(MemberComponents.begin(), MemberComponents.end(),
426+
getTrailingObjects<ComponentIdentTypeRepr *>());
423427
}
424428

425429
public:
426-
static CompoundIdentTypeRepr *create(const ASTContext &Ctx,
427-
ArrayRef<ComponentIdentTypeRepr*> Components);
430+
static CompoundIdentTypeRepr *
431+
create(const ASTContext &Ctx, TypeRepr *Base,
432+
ArrayRef<ComponentIdentTypeRepr *> MemberComponents);
428433

429-
TypeRepr *getBaseComponent() const { return getComponents().front(); }
430-
431-
ArrayRef<ComponentIdentTypeRepr*> getComponents() const {
432-
return {getTrailingObjects<ComponentIdentTypeRepr*>(),
433-
Bits.CompoundIdentTypeRepr.NumComponents};
434-
}
434+
TypeRepr *getBaseComponent() const { return Base; }
435435

436436
ArrayRef<ComponentIdentTypeRepr *> getMemberComponents() const {
437-
return getComponents().slice(1);
437+
return {getTrailingObjects<ComponentIdentTypeRepr *>(),
438+
Bits.CompoundIdentTypeRepr.NumMemberComponents};
438439
}
439440

440441
ComponentIdentTypeRepr *getLastComponent() const {

lib/AST/CASTBridging.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ void *IdentTypeRepr_create(void *ctx, BridgedArrayRef bridgedComponents) {
414414
return components.front();
415415
}
416416

417-
return CompoundIdentTypeRepr::create(Context, components);
417+
return CompoundIdentTypeRepr::create(Context, components.front(),
418+
components.drop_front());
418419
}
419420

420421
void *CompositionTypeRepr_create(void *ctx, BridgedArrayRef types,

lib/AST/Expr.cpp

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,23 +2144,18 @@ TypeExpr *TypeExpr::createForMemberDecl(DeclNameLoc ParentNameLoc,
21442144
assert(ParentNameLoc.isValid());
21452145
assert(NameLoc.isValid());
21462146

2147-
// Create a new list of components.
2148-
SmallVector<ComponentIdentTypeRepr *, 2> Components;
2149-
2150-
// The first component is the parent type.
2147+
// The base component is the parent type.
21512148
auto *ParentComp = new (C) SimpleIdentTypeRepr(ParentNameLoc,
21522149
Parent->createNameRef());
21532150
ParentComp->setValue(Parent, nullptr);
2154-
Components.push_back(ParentComp);
21552151

2156-
// The second component is the member we just found.
2157-
auto *NewComp = new (C) SimpleIdentTypeRepr(NameLoc,
2158-
Decl->createNameRef());
2159-
NewComp->setValue(Decl, nullptr);
2160-
Components.push_back(NewComp);
2152+
// The member component is the member we just found.
2153+
auto *MemberComp =
2154+
new (C) SimpleIdentTypeRepr(NameLoc, Decl->createNameRef());
2155+
MemberComp->setValue(Decl, nullptr);
21612156

2162-
auto *NewTypeRepr = CompoundIdentTypeRepr::create(C, Components);
2163-
return new (C) TypeExpr(NewTypeRepr);
2157+
auto *TR = CompoundIdentTypeRepr::create(C, ParentComp, {MemberComp});
2158+
return new (C) TypeExpr(TR);
21642159
}
21652160

21662161
TypeExpr *TypeExpr::createForMemberDecl(IdentTypeRepr *ParentTR,
@@ -2170,43 +2165,32 @@ TypeExpr *TypeExpr::createForMemberDecl(IdentTypeRepr *ParentTR,
21702165

21712166
// Create a new list of components.
21722167
SmallVector<ComponentIdentTypeRepr *, 2> Components;
2173-
if (auto *Comp = dyn_cast<ComponentIdentTypeRepr>(ParentTR)) {
2174-
Components.push_back(Comp);
2175-
} else {
2176-
auto OldComps = cast<CompoundIdentTypeRepr>(ParentTR)->getComponents();
2177-
Components.append(OldComps.begin(), OldComps.end());
2168+
if (auto *Compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
2169+
auto MemberComps = Compound->getMemberComponents();
2170+
Components.append(MemberComps.begin(), MemberComps.end());
21782171
}
21792172

2180-
assert(!Components.empty());
2181-
21822173
// Add a new component for the member we just found.
21832174
auto *NewComp = new (C) SimpleIdentTypeRepr(NameLoc, Decl->createNameRef());
21842175
NewComp->setValue(Decl, nullptr);
21852176
Components.push_back(NewComp);
21862177

2187-
auto *NewTypeRepr = CompoundIdentTypeRepr::create(C, Components);
2188-
return new (C) TypeExpr(NewTypeRepr);
2178+
auto *TR = CompoundIdentTypeRepr::create(C, ParentTR->getBaseComponent(),
2179+
Components);
2180+
return new (C) TypeExpr(TR);
21892181
}
21902182

21912183
TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
21922184
ArrayRef<TypeRepr*> Args,
21932185
SourceRange AngleLocs,
21942186
ASTContext &C) {
2195-
// Create a new list of components.
2196-
SmallVector<ComponentIdentTypeRepr *, 2> components;
2197-
if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(ParentTR)) {
2198-
components.push_back(comp);
2199-
} else {
2200-
auto oldComps = cast<CompoundIdentTypeRepr>(ParentTR)->getComponents();
2201-
components.append(oldComps.begin(), oldComps.end());
2202-
}
2187+
auto *lastComp = ParentTR->getLastComponent();
22032188

2204-
auto *last = components.back();
2205-
components.pop_back();
2189+
if (!isa<SimpleIdentTypeRepr>(lastComp) || !lastComp->getBoundDecl())
2190+
return nullptr;
22062191

2207-
if (isa<SimpleIdentTypeRepr>(last) &&
2208-
last->getBoundDecl()) {
2209-
if (isa<TypeAliasDecl>(last->getBoundDecl())) {
2192+
if (isa<TypeAliasDecl>(lastComp->getBoundDecl())) {
2193+
if (auto *compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
22102194
// If any of our parent types are unbound, bail out and let
22112195
// the constraint solver can infer generic parameters for them.
22122196
//
@@ -2219,33 +2203,50 @@ TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
22192203
//
22202204
// FIXME: Once we can model generic typealiases properly, rip
22212205
// this out.
2222-
for (auto *component : components) {
2223-
auto *componentDecl = dyn_cast_or_null<GenericTypeDecl>(
2224-
component->getBoundDecl());
2206+
auto isUnboundGenericComponent = [](ComponentIdentTypeRepr *TR) -> bool {
2207+
if (isa<SimpleIdentTypeRepr>(TR)) {
2208+
auto *decl = dyn_cast_or_null<GenericTypeDecl>(TR->getBoundDecl());
2209+
if (decl && decl->isGeneric())
2210+
return true;
2211+
}
22252212

2226-
if (isa<SimpleIdentTypeRepr>(component) &&
2227-
componentDecl &&
2228-
componentDecl->isGeneric())
2213+
return false;
2214+
};
2215+
2216+
for (auto *comp : compound->getMemberComponents().drop_back()) {
2217+
if (isUnboundGenericComponent(comp))
22292218
return nullptr;
22302219
}
2231-
}
22322220

2233-
auto *genericComp = GenericIdentTypeRepr::create(C,
2234-
last->getNameLoc(), last->getNameRef(),
2235-
Args, AngleLocs);
2236-
genericComp->setValue(last->getBoundDecl(), last->getDeclContext());
2237-
components.push_back(genericComp);
2238-
2239-
TypeRepr *genericRepr = nullptr;
2240-
if (components.size() == 1) {
2241-
genericRepr = components.front();
2242-
} else {
2243-
genericRepr = CompoundIdentTypeRepr::create(C, components);
2221+
if (auto *identBase =
2222+
dyn_cast<ComponentIdentTypeRepr>(compound->getBaseComponent())) {
2223+
if (isUnboundGenericComponent(identBase))
2224+
return nullptr;
2225+
}
22442226
}
2245-
return new (C) TypeExpr(genericRepr);
22462227
}
22472228

2248-
return nullptr;
2229+
auto *genericComp = GenericIdentTypeRepr::create(
2230+
C, lastComp->getNameLoc(), lastComp->getNameRef(), Args, AngleLocs);
2231+
genericComp->setValue(lastComp->getBoundDecl(), lastComp->getDeclContext());
2232+
2233+
TypeRepr *TR = nullptr;
2234+
if (auto *compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
2235+
auto oldMemberComps = compound->getMemberComponents().drop_back();
2236+
2237+
// Create a new list of member components, replacing the last one with the
2238+
// new specialized one.
2239+
SmallVector<ComponentIdentTypeRepr *, 2> newMemberComps;
2240+
newMemberComps.append(oldMemberComps.begin(), oldMemberComps.end());
2241+
newMemberComps.push_back(genericComp);
2242+
2243+
TR = CompoundIdentTypeRepr::create(C, compound->getBaseComponent(),
2244+
newMemberComps);
2245+
} else {
2246+
TR = genericComp;
2247+
}
2248+
2249+
return new (C) TypeExpr(TR);
22492250
}
22502251

22512252
// Create an implicit TypeExpr, with location information even though it

lib/AST/NameLookup.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3085,14 +3085,11 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
30853085
assocType->getDescriptiveKind(),
30863086
assocType->getName());
30873087

3088-
ComponentIdentTypeRepr *components[2] = {
3089-
new (ctx) SimpleIdentTypeRepr(identTypeRepr->getNameLoc(),
3090-
DeclNameRef(moduleName)),
3091-
identTypeRepr
3092-
};
3088+
auto *baseComp = new (ctx) SimpleIdentTypeRepr(
3089+
identTypeRepr->getNameLoc(), DeclNameRef(moduleName));
30933090

3094-
auto *newTE = new (ctx)
3095-
TypeExpr(CompoundIdentTypeRepr::create(ctx, components));
3091+
auto *newTE = new (ctx) TypeExpr(
3092+
CompoundIdentTypeRepr::create(ctx, baseComp, {identTypeRepr}));
30963093
attr->resetTypeInformation(newTE);
30973094
return nominal;
30983095
}

lib/AST/TypeRepr.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,13 @@ GenericIdentTypeRepr *GenericIdentTypeRepr::create(const ASTContext &C,
382382
return new (mem) GenericIdentTypeRepr(Loc, Id, GenericArgs, AngleBrackets);
383383
}
384384

385-
CompoundIdentTypeRepr *CompoundIdentTypeRepr::create(const ASTContext &C,
386-
ArrayRef<ComponentIdentTypeRepr*> Components) {
387-
auto size = totalSizeToAlloc<ComponentIdentTypeRepr*>(Components.size());
385+
CompoundIdentTypeRepr *CompoundIdentTypeRepr::create(
386+
const ASTContext &C, TypeRepr *Base,
387+
ArrayRef<ComponentIdentTypeRepr *> MemberComponents) {
388+
auto size =
389+
totalSizeToAlloc<ComponentIdentTypeRepr *>(MemberComponents.size());
388390
auto mem = C.Allocate(size, alignof(CompoundIdentTypeRepr));
389-
return new (mem) CompoundIdentTypeRepr(Components);
391+
return new (mem) CompoundIdentTypeRepr(Base, MemberComponents);
390392
}
391393

392394
SILBoxTypeRepr *SILBoxTypeRepr::create(ASTContext &C,

lib/Parse/ParseType.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,9 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) {
750750
if (ComponentsR.size() == 1) {
751751
ITR = ComponentsR.front();
752752
} else {
753-
ITR = CompoundIdentTypeRepr::create(Context, ComponentsR);
753+
ArrayRef<ComponentIdentTypeRepr *> Comps(ComponentsR);
754+
ITR = CompoundIdentTypeRepr::create(Context, Comps.front(),
755+
Comps.drop_front());
754756
}
755757
}
756758

lib/Sema/TypeCheckPattern.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,9 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
486486
if (components.size() == 1) {
487487
repr = components.front();
488488
} else {
489-
repr = CompoundIdentTypeRepr::create(Context, components);
489+
ArrayRef<ComponentIdentTypeRepr *> comps(components);
490+
repr = CompoundIdentTypeRepr::create(Context, comps.front(),
491+
comps.drop_front());
490492
}
491493

492494
// See if the repr resolves to a type.
@@ -612,7 +614,9 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
612614
if (components.size() == 1) {
613615
prefixRepr = components.front();
614616
} else {
615-
prefixRepr = CompoundIdentTypeRepr::create(Context, components);
617+
ArrayRef<ComponentIdentTypeRepr *> comps(components);
618+
prefixRepr = CompoundIdentTypeRepr::create(Context, comps.front(),
619+
comps.drop_front());
616620
}
617621

618622
// See first if the entire repr resolves to a type.

0 commit comments

Comments
 (0)