Skip to content

Commit cf1572c

Browse files
committed
AST: Add ASTContext::TheSelfType for convenience
1 parent 5d36643 commit cf1572c

File tree

13 files changed

+24
-37
lines changed

13 files changed

+24
-37
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,9 @@ class ASTContext final {
10511051
const CanType TheUnconstrainedAnyType; /// This is 'any ~Copyable & ~Escapable',
10521052
/// the empty protocol composition
10531053
/// without any implicit constraints.
1054+
const CanGenericTypeParamType TheSelfType; /// The protocol 'Self' type;
1055+
/// a generic parameter with
1056+
/// depth 0 index 0
10541057
#define SINGLETON_TYPE(SHORT_ID, ID) \
10551058
const CanType The##SHORT_ID##Type;
10561059
#include "swift/AST/TypeNodes.def"

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ ASTContext::ASTContext(
817817
TheAnyType(ProtocolCompositionType::theAnyType(*this)),
818818
TheUnconstrainedAnyType(
819819
ProtocolCompositionType::theUnconstrainedAnyType(*this)),
820+
TheSelfType(CanGenericTypeParamType(
821+
GenericTypeParamType::getType(0, 0, *this))),
820822
#define SINGLETON_TYPE(SHORT_ID, ID) \
821823
The##SHORT_ID##Type(new (*this, AllocationArena::Permanent) \
822824
ID##Type(*this)),
@@ -6639,8 +6641,7 @@ CanGenericSignature ASTContext::getSingleGenericParameterSignature() const {
66396641
if (auto theSig = getImpl().SingleGenericParameterSignature)
66406642
return theSig;
66416643

6642-
auto param = GenericTypeParamType::getType(/*depth*/ 0, /*index*/ 0, *this);
6643-
auto sig = GenericSignature::get(param, { });
6644+
auto sig = GenericSignature::get({TheSelfType}, { });
66446645
auto canonicalSig = CanGenericSignature(sig);
66456646
getImpl().SingleGenericParameterSignature = canonicalSig;
66466647
return canonicalSig;

lib/AST/ASTMangler.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ std::string ASTMangler::mangleKeyPathGetterThunkHelper(
395395
sub = sub.transformRec([](Type t) -> std::optional<Type> {
396396
if (auto *openedExistential = t->getAs<ExistentialArchetypeType>()) {
397397
auto &ctx = openedExistential->getASTContext();
398-
return GenericTypeParamType::getType(0, 0, ctx);
398+
return ctx.TheSelfType;
399399
}
400400
return std::nullopt;
401401
});
@@ -431,7 +431,7 @@ std::string ASTMangler::mangleKeyPathSetterThunkHelper(
431431
sub = sub.transformRec([](Type t) -> std::optional<Type> {
432432
if (auto *openedExistential = t->getAs<ExistentialArchetypeType>()) {
433433
auto &ctx = openedExistential->getASTContext();
434-
return GenericTypeParamType::getType(0, 0, ctx);
434+
return ctx.TheSelfType;
435435
}
436436
return std::nullopt;
437437
});
@@ -5274,15 +5274,11 @@ static void extractExistentialInverseRequirements(
52745274

52755275
auto &ctx = PCT->getASTContext();
52765276

5277-
// Form a parameter referring to the existential's Self.
5278-
auto existentialSelf =
5279-
GenericTypeParamType::getType(/*depth=*/0, /*index=*/0, ctx);
5280-
52815277
for (auto ip : PCT->getInverses()) {
52825278
auto *proto = ctx.getProtocol(getKnownProtocolKind(ip));
52835279
assert(proto);
52845280
ASSERT(!getABIDecl(proto) && "can't use @abi on inverse protocols");
5285-
inverses.push_back({existentialSelf, proto, SourceLoc()});
5281+
inverses.push_back({ctx.TheSelfType, proto, SourceLoc()});
52865282
}
52875283
}
52885284

lib/AST/RequirementEnvironment.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ RequirementEnvironment::RequirementEnvironment(
9898
// type.
9999
if (type->isEqual(selfType)) {
100100
if (covariantSelf)
101-
return GenericTypeParamType::getType(/*depth=*/0, /*index=*/0, ctx);
101+
return ctx.TheSelfType;
102102
return substConcreteType;
103103
}
104104
// Other requirement generic parameters map 1:1 with their depth
@@ -173,8 +173,7 @@ RequirementEnvironment::RequirementEnvironment(
173173
// If the conforming type is a class, add a class-constrained 'Self'
174174
// parameter.
175175
if (covariantSelf) {
176-
auto paramTy = GenericTypeParamType::getType(/*depth=*/0, /*index=*/0, ctx);
177-
genericParamTypes.push_back(paramTy);
176+
genericParamTypes.push_back(ctx.TheSelfType);
178177
}
179178

180179
// Now, add all generic parameters from the conforming type.
@@ -188,8 +187,7 @@ RequirementEnvironment::RequirementEnvironment(
188187
// Next, add requirements.
189188
SmallVector<Requirement, 2> requirements;
190189
if (covariantSelf) {
191-
auto paramTy = GenericTypeParamType::getType(/*depth=*/0, /*index=*/0, ctx);
192-
Requirement reqt(RequirementKind::Superclass, paramTy, substConcreteType);
190+
Requirement reqt(RequirementKind::Superclass, ctx.TheSelfType, substConcreteType);
193191
requirements.push_back(reqt);
194192
}
195193

lib/AST/RequirementMachine/InterfaceType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ getTypeForSymbolRange(const Symbol *begin, const Symbol *end,
281281
continue;
282282

283283
case Symbol::Kind::Protocol:
284-
handleRoot(GenericTypeParamType::getType(0, 0, ctx.getASTContext()));
284+
handleRoot(ctx.getASTContext().TheSelfType);
285285
continue;
286286

287287
case Symbol::Kind::AssociatedType:
288-
handleRoot(GenericTypeParamType::getType(0, 0, ctx.getASTContext()));
288+
handleRoot(ctx.getASTContext().TheSelfType);
289289

290290
// An associated type symbol at the root means we have a dependent
291291
// member type rooted at Self; handle the associated type below.

lib/IRGen/GenDistributed.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ static CanSILFunctionType getAccessorType(IRGenModule &IGM) {
277277
SmallVector<GenericFunctionType::Param, 8> parameters;
278278

279279
// A generic parameter that represents instance of invocation decoder.
280-
auto *decoderType =
281-
GenericTypeParamType::getType(/*depth=*/ 0, /*index=*/ 0, Context);
280+
auto decoderType = Context.TheSelfType;
282281

283282
// decoder
284283
parameters.push_back(GenericFunctionType::Param(

lib/IRGen/GenHeap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ class FixedBoxTypeInfoBase : public BoxTypeInfo {
15871587
.transformRec([](Type t) -> std::optional<Type> {
15881588
if (auto *openedExistential = t->getAs<ExistentialArchetypeType>()) {
15891589
auto &ctx = openedExistential->getASTContext();
1590-
return GenericTypeParamType::getType(0, 0, ctx);
1590+
return ctx.TheSelfType;
15911591
}
15921592
return std::nullopt;
15931593
})

lib/IRGen/GenKeyPath.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ emitKeyPathComponent(IRGenModule &IGM,
783783
if (auto *openedExistential =
784784
t->getAs<ExistentialArchetypeType>()) {
785785
auto &ctx = openedExistential->getASTContext();
786-
return GenericTypeParamType::getType(0, 0, ctx);
786+
return ctx.TheSelfType;
787787
}
788788
return std::nullopt;
789789
})

lib/SILGen/SILGenBuiltin.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,8 +1667,7 @@ static ManagedValue emitCreateAsyncTask(SILGenFunction &SGF, SILLocation loc,
16671667
.build();
16681668

16691669
auto genericSig = subs.getGenericSignature().getCanonicalSignature();
1670-
auto genericResult = GenericTypeParamType::getType(/*depth*/ 0, /*index*/ 0,
1671-
SGF.getASTContext());
1670+
auto genericResult = SGF.getASTContext().TheSelfType;
16721671

16731672
// <T> () async throws -> T
16741673
CanType functionTy =

lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,9 +1576,7 @@ RValueEmitter::visitConditionalBridgeFromObjCExpr(
15761576
auto conversion = cast<FuncDecl>(conversionRef.getDecl());
15771577
auto subs = conversionRef.getSubstitutions();
15781578

1579-
auto nativeType = Type(GenericTypeParamType::getType(/*depth*/ 0, /*index*/ 0,
1580-
SGF.getASTContext()))
1581-
.subst(subs);
1579+
auto nativeType = Type(SGF.getASTContext().TheSelfType).subst(subs);
15821580

15831581
auto metatypeType = SGF.getLoweredType(MetatypeType::get(nativeType));
15841582
auto metatype = ManagedValue::forObjectRValueWithoutOwnership(
@@ -4153,9 +4151,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
41534151
auto formalCanTy = formalTy->getCanonicalType();
41544152

41554153
// Get the Equatable conformance from the Hashable conformance.
4156-
auto equatable = hashable.getAssociatedConformance(
4157-
GenericTypeParamType::getType(/*depth*/ 0, /*index*/ 0, C),
4158-
equatableProtocol);
4154+
auto equatable = hashable.getAssociatedConformance(C.TheSelfType, equatableProtocol);
41594155

41604156
assert(equatable.isAbstract() == hashable.isAbstract());
41614157
if (equatable.isConcrete())

0 commit comments

Comments
 (0)