Skip to content

Commit 0c3a72d

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

File tree

4 files changed

+90
-80
lines changed

4 files changed

+90
-80
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 19 additions & 15 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,
@@ -412,28 +412,32 @@ class CompoundIdentTypeRepr final : public IdentTypeRepr,
412412
ComponentIdentTypeRepr *> {
413413
friend TrailingObjects;
414414

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

424428
public:
425-
static CompoundIdentTypeRepr *create(const ASTContext &Ctx,
426-
ArrayRef<ComponentIdentTypeRepr*> Components);
429+
static CompoundIdentTypeRepr *
430+
create(const ASTContext &Ctx, TypeRepr *Base,
431+
ArrayRef<ComponentIdentTypeRepr *> MemberComponents);
427432

428-
TypeRepr *getBaseComponent() const { return getComponents().front(); }
433+
static CompoundIdentTypeRepr *
434+
create(const ASTContext &Ctx, ArrayRef<ComponentIdentTypeRepr *> Components);
429435

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

435438
ArrayRef<ComponentIdentTypeRepr *> getMemberComponents() const {
436-
return getComponents().slice(1);
439+
return {getTrailingObjects<ComponentIdentTypeRepr *>(),
440+
Bits.CompoundIdentTypeRepr.NumMemberComponents};
437441
}
438442

439443
ComponentIdentTypeRepr *getLastComponent() const {

lib/AST/Expr.cpp

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,23 +2210,18 @@ TypeExpr *TypeExpr::createForMemberDecl(DeclNameLoc ParentNameLoc,
22102210
assert(ParentNameLoc.isValid());
22112211
assert(NameLoc.isValid());
22122212

2213-
// Create a new list of components.
2214-
SmallVector<ComponentIdentTypeRepr *, 2> Components;
2215-
2216-
// The first component is the parent type.
2213+
// The base component is the parent type.
22172214
auto *ParentComp = new (C) SimpleIdentTypeRepr(ParentNameLoc,
22182215
Parent->createNameRef());
22192216
ParentComp->setValue(Parent, nullptr);
2220-
Components.push_back(ParentComp);
22212217

2222-
// The second component is the member we just found.
2223-
auto *NewComp = new (C) SimpleIdentTypeRepr(NameLoc,
2224-
Decl->createNameRef());
2225-
NewComp->setValue(Decl, nullptr);
2226-
Components.push_back(NewComp);
2218+
// The member component is the member we just found.
2219+
auto *MemberComp =
2220+
new (C) SimpleIdentTypeRepr(NameLoc, Decl->createNameRef());
2221+
MemberComp->setValue(Decl, nullptr);
22272222

2228-
auto *NewTypeRepr = CompoundIdentTypeRepr::create(C, Components);
2229-
return new (C) TypeExpr(NewTypeRepr);
2223+
auto *TR = CompoundIdentTypeRepr::create(C, ParentComp, {MemberComp});
2224+
return new (C) TypeExpr(TR);
22302225
}
22312226

22322227
TypeExpr *TypeExpr::createForMemberDecl(IdentTypeRepr *ParentTR,
@@ -2236,43 +2231,32 @@ TypeExpr *TypeExpr::createForMemberDecl(IdentTypeRepr *ParentTR,
22362231

22372232
// Create a new list of components.
22382233
SmallVector<ComponentIdentTypeRepr *, 2> Components;
2239-
if (auto *Comp = dyn_cast<ComponentIdentTypeRepr>(ParentTR)) {
2240-
Components.push_back(Comp);
2241-
} else {
2242-
auto OldComps = cast<CompoundIdentTypeRepr>(ParentTR)->getComponents();
2243-
Components.append(OldComps.begin(), OldComps.end());
2234+
if (auto *Compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
2235+
auto MemberComps = Compound->getMemberComponents();
2236+
Components.append(MemberComps.begin(), MemberComps.end());
22442237
}
22452238

2246-
assert(!Components.empty());
2247-
22482239
// Add a new component for the member we just found.
22492240
auto *NewComp = new (C) SimpleIdentTypeRepr(NameLoc, Decl->createNameRef());
22502241
NewComp->setValue(Decl, nullptr);
22512242
Components.push_back(NewComp);
22522243

2253-
auto *NewTypeRepr = CompoundIdentTypeRepr::create(C, Components);
2254-
return new (C) TypeExpr(NewTypeRepr);
2244+
auto *TR = CompoundIdentTypeRepr::create(C, ParentTR->getBaseComponent(),
2245+
Components);
2246+
return new (C) TypeExpr(TR);
22552247
}
22562248

22572249
TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
22582250
ArrayRef<TypeRepr*> Args,
22592251
SourceRange AngleLocs,
22602252
ASTContext &C) {
2261-
// Create a new list of components.
2262-
SmallVector<ComponentIdentTypeRepr *, 2> components;
2263-
if (auto *comp = dyn_cast<ComponentIdentTypeRepr>(ParentTR)) {
2264-
components.push_back(comp);
2265-
} else {
2266-
auto oldComps = cast<CompoundIdentTypeRepr>(ParentTR)->getComponents();
2267-
components.append(oldComps.begin(), oldComps.end());
2268-
}
2253+
auto *lastComp = ParentTR->getLastComponent();
22692254

2270-
auto *last = components.back();
2271-
components.pop_back();
2255+
if (!isa<SimpleIdentTypeRepr>(lastComp) || !lastComp->getBoundDecl())
2256+
return nullptr;
22722257

2273-
if (isa<SimpleIdentTypeRepr>(last) &&
2274-
last->getBoundDecl()) {
2275-
if (isa<TypeAliasDecl>(last->getBoundDecl())) {
2258+
if (isa<TypeAliasDecl>(lastComp->getBoundDecl())) {
2259+
if (auto *compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
22762260
// If any of our parent types are unbound, bail out and let
22772261
// the constraint solver can infer generic parameters for them.
22782262
//
@@ -2285,33 +2269,50 @@ TypeExpr *TypeExpr::createForSpecializedDecl(IdentTypeRepr *ParentTR,
22852269
//
22862270
// FIXME: Once we can model generic typealiases properly, rip
22872271
// this out.
2288-
for (auto *component : components) {
2289-
auto *componentDecl = dyn_cast_or_null<GenericTypeDecl>(
2290-
component->getBoundDecl());
2272+
auto isUnboundGenericComponent = [](ComponentIdentTypeRepr *TR) -> bool {
2273+
if (isa<SimpleIdentTypeRepr>(TR)) {
2274+
auto *decl = dyn_cast_or_null<GenericTypeDecl>(TR->getBoundDecl());
2275+
if (decl && decl->isGeneric())
2276+
return true;
2277+
}
22912278

2292-
if (isa<SimpleIdentTypeRepr>(component) &&
2293-
componentDecl &&
2294-
componentDecl->isGeneric())
2279+
return false;
2280+
};
2281+
2282+
for (auto *comp : compound->getMemberComponents().drop_back()) {
2283+
if (isUnboundGenericComponent(comp))
22952284
return nullptr;
22962285
}
2297-
}
22982286

2299-
auto *genericComp = GenericIdentTypeRepr::create(C,
2300-
last->getNameLoc(), last->getNameRef(),
2301-
Args, AngleLocs);
2302-
genericComp->setValue(last->getBoundDecl(), last->getDeclContext());
2303-
components.push_back(genericComp);
2304-
2305-
TypeRepr *genericRepr = nullptr;
2306-
if (components.size() == 1) {
2307-
genericRepr = components.front();
2308-
} else {
2309-
genericRepr = CompoundIdentTypeRepr::create(C, components);
2287+
if (auto *identBase =
2288+
dyn_cast<ComponentIdentTypeRepr>(compound->getBaseComponent())) {
2289+
if (isUnboundGenericComponent(identBase))
2290+
return nullptr;
2291+
}
23102292
}
2311-
return new (C) TypeExpr(genericRepr);
23122293
}
23132294

2314-
return nullptr;
2295+
auto *genericComp = GenericIdentTypeRepr::create(
2296+
C, lastComp->getNameLoc(), lastComp->getNameRef(), Args, AngleLocs);
2297+
genericComp->setValue(lastComp->getBoundDecl(), lastComp->getDeclContext());
2298+
2299+
TypeRepr *TR = nullptr;
2300+
if (auto *compound = dyn_cast<CompoundIdentTypeRepr>(ParentTR)) {
2301+
auto oldMemberComps = compound->getMemberComponents().drop_back();
2302+
2303+
// Create a new list of member components, replacing the last one with the
2304+
// new specialized one.
2305+
SmallVector<ComponentIdentTypeRepr *, 2> newMemberComps;
2306+
newMemberComps.append(oldMemberComps.begin(), oldMemberComps.end());
2307+
newMemberComps.push_back(genericComp);
2308+
2309+
TR = CompoundIdentTypeRepr::create(C, compound->getBaseComponent(),
2310+
newMemberComps);
2311+
} else {
2312+
TR = genericComp;
2313+
}
2314+
2315+
return new (C) TypeExpr(TR);
23152316
}
23162317

23172318
// 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
@@ -3081,14 +3081,11 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
30813081
assocType->getDescriptiveKind(),
30823082
assocType->getName());
30833083

3084-
ComponentIdentTypeRepr *components[2] = {
3085-
new (ctx) SimpleIdentTypeRepr(identTypeRepr->getNameLoc(),
3086-
DeclNameRef(moduleName)),
3087-
identTypeRepr
3088-
};
3084+
auto *baseComp = new (ctx) SimpleIdentTypeRepr(
3085+
identTypeRepr->getNameLoc(), DeclNameRef(moduleName));
30893086

3090-
auto *newTE = new (ctx)
3091-
TypeExpr(CompoundIdentTypeRepr::create(ctx, components));
3087+
auto *newTE = new (ctx) TypeExpr(
3088+
CompoundIdentTypeRepr::create(ctx, baseComp, {identTypeRepr}));
30923089
attr->resetTypeInformation(newTE);
30933090
return nominal;
30943091
}

lib/AST/TypeRepr.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,19 @@ 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);
392+
}
393+
394+
CompoundIdentTypeRepr *
395+
CompoundIdentTypeRepr::create(const ASTContext &Ctx,
396+
ArrayRef<ComponentIdentTypeRepr *> Components) {
397+
return create(Ctx, Components.front(), Components.drop_front());
390398
}
391399

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

0 commit comments

Comments
 (0)