AST: Add ASTContext::TheSelfType for convenience

This commit is contained in:
Slava Pestov
2025-04-25 13:32:04 -04:00
parent 5d36643af4
commit cf1572c65b
13 changed files with 24 additions and 37 deletions

View File

@@ -1051,6 +1051,9 @@ public:
const CanType TheUnconstrainedAnyType; /// This is 'any ~Copyable & ~Escapable',
/// the empty protocol composition
/// without any implicit constraints.
const CanGenericTypeParamType TheSelfType; /// The protocol 'Self' type;
/// a generic parameter with
/// depth 0 index 0
#define SINGLETON_TYPE(SHORT_ID, ID) \
const CanType The##SHORT_ID##Type;
#include "swift/AST/TypeNodes.def"

View File

@@ -817,6 +817,8 @@ ASTContext::ASTContext(
TheAnyType(ProtocolCompositionType::theAnyType(*this)),
TheUnconstrainedAnyType(
ProtocolCompositionType::theUnconstrainedAnyType(*this)),
TheSelfType(CanGenericTypeParamType(
GenericTypeParamType::getType(0, 0, *this))),
#define SINGLETON_TYPE(SHORT_ID, ID) \
The##SHORT_ID##Type(new (*this, AllocationArena::Permanent) \
ID##Type(*this)),
@@ -6639,8 +6641,7 @@ CanGenericSignature ASTContext::getSingleGenericParameterSignature() const {
if (auto theSig = getImpl().SingleGenericParameterSignature)
return theSig;
auto param = GenericTypeParamType::getType(/*depth*/ 0, /*index*/ 0, *this);
auto sig = GenericSignature::get(param, { });
auto sig = GenericSignature::get({TheSelfType}, { });
auto canonicalSig = CanGenericSignature(sig);
getImpl().SingleGenericParameterSignature = canonicalSig;
return canonicalSig;

View File

@@ -395,7 +395,7 @@ std::string ASTMangler::mangleKeyPathGetterThunkHelper(
sub = sub.transformRec([](Type t) -> std::optional<Type> {
if (auto *openedExistential = t->getAs<ExistentialArchetypeType>()) {
auto &ctx = openedExistential->getASTContext();
return GenericTypeParamType::getType(0, 0, ctx);
return ctx.TheSelfType;
}
return std::nullopt;
});
@@ -431,7 +431,7 @@ std::string ASTMangler::mangleKeyPathSetterThunkHelper(
sub = sub.transformRec([](Type t) -> std::optional<Type> {
if (auto *openedExistential = t->getAs<ExistentialArchetypeType>()) {
auto &ctx = openedExistential->getASTContext();
return GenericTypeParamType::getType(0, 0, ctx);
return ctx.TheSelfType;
}
return std::nullopt;
});
@@ -5274,15 +5274,11 @@ static void extractExistentialInverseRequirements(
auto &ctx = PCT->getASTContext();
// Form a parameter referring to the existential's Self.
auto existentialSelf =
GenericTypeParamType::getType(/*depth=*/0, /*index=*/0, ctx);
for (auto ip : PCT->getInverses()) {
auto *proto = ctx.getProtocol(getKnownProtocolKind(ip));
assert(proto);
ASSERT(!getABIDecl(proto) && "can't use @abi on inverse protocols");
inverses.push_back({existentialSelf, proto, SourceLoc()});
inverses.push_back({ctx.TheSelfType, proto, SourceLoc()});
}
}

View File

@@ -98,7 +98,7 @@ RequirementEnvironment::RequirementEnvironment(
// type.
if (type->isEqual(selfType)) {
if (covariantSelf)
return GenericTypeParamType::getType(/*depth=*/0, /*index=*/0, ctx);
return ctx.TheSelfType;
return substConcreteType;
}
// Other requirement generic parameters map 1:1 with their depth
@@ -173,8 +173,7 @@ RequirementEnvironment::RequirementEnvironment(
// If the conforming type is a class, add a class-constrained 'Self'
// parameter.
if (covariantSelf) {
auto paramTy = GenericTypeParamType::getType(/*depth=*/0, /*index=*/0, ctx);
genericParamTypes.push_back(paramTy);
genericParamTypes.push_back(ctx.TheSelfType);
}
// Now, add all generic parameters from the conforming type.
@@ -188,8 +187,7 @@ RequirementEnvironment::RequirementEnvironment(
// Next, add requirements.
SmallVector<Requirement, 2> requirements;
if (covariantSelf) {
auto paramTy = GenericTypeParamType::getType(/*depth=*/0, /*index=*/0, ctx);
Requirement reqt(RequirementKind::Superclass, paramTy, substConcreteType);
Requirement reqt(RequirementKind::Superclass, ctx.TheSelfType, substConcreteType);
requirements.push_back(reqt);
}

View File

@@ -281,11 +281,11 @@ getTypeForSymbolRange(const Symbol *begin, const Symbol *end,
continue;
case Symbol::Kind::Protocol:
handleRoot(GenericTypeParamType::getType(0, 0, ctx.getASTContext()));
handleRoot(ctx.getASTContext().TheSelfType);
continue;
case Symbol::Kind::AssociatedType:
handleRoot(GenericTypeParamType::getType(0, 0, ctx.getASTContext()));
handleRoot(ctx.getASTContext().TheSelfType);
// An associated type symbol at the root means we have a dependent
// member type rooted at Self; handle the associated type below.

View File

@@ -277,8 +277,7 @@ static CanSILFunctionType getAccessorType(IRGenModule &IGM) {
SmallVector<GenericFunctionType::Param, 8> parameters;
// A generic parameter that represents instance of invocation decoder.
auto *decoderType =
GenericTypeParamType::getType(/*depth=*/ 0, /*index=*/ 0, Context);
auto decoderType = Context.TheSelfType;
// decoder
parameters.push_back(GenericFunctionType::Param(

View File

@@ -1587,7 +1587,7 @@ public:
.transformRec([](Type t) -> std::optional<Type> {
if (auto *openedExistential = t->getAs<ExistentialArchetypeType>()) {
auto &ctx = openedExistential->getASTContext();
return GenericTypeParamType::getType(0, 0, ctx);
return ctx.TheSelfType;
}
return std::nullopt;
})

View File

@@ -783,7 +783,7 @@ emitKeyPathComponent(IRGenModule &IGM,
if (auto *openedExistential =
t->getAs<ExistentialArchetypeType>()) {
auto &ctx = openedExistential->getASTContext();
return GenericTypeParamType::getType(0, 0, ctx);
return ctx.TheSelfType;
}
return std::nullopt;
})

View File

@@ -1667,8 +1667,7 @@ static ManagedValue emitCreateAsyncTask(SILGenFunction &SGF, SILLocation loc,
.build();
auto genericSig = subs.getGenericSignature().getCanonicalSignature();
auto genericResult = GenericTypeParamType::getType(/*depth*/ 0, /*index*/ 0,
SGF.getASTContext());
auto genericResult = SGF.getASTContext().TheSelfType;
// <T> () async throws -> T
CanType functionTy =

View File

@@ -1576,9 +1576,7 @@ RValueEmitter::visitConditionalBridgeFromObjCExpr(
auto conversion = cast<FuncDecl>(conversionRef.getDecl());
auto subs = conversionRef.getSubstitutions();
auto nativeType = Type(GenericTypeParamType::getType(/*depth*/ 0, /*index*/ 0,
SGF.getASTContext()))
.subst(subs);
auto nativeType = Type(SGF.getASTContext().TheSelfType).subst(subs);
auto metatypeType = SGF.getLoweredType(MetatypeType::get(nativeType));
auto metatype = ManagedValue::forObjectRValueWithoutOwnership(
@@ -4153,9 +4151,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
auto formalCanTy = formalTy->getCanonicalType();
// Get the Equatable conformance from the Hashable conformance.
auto equatable = hashable.getAssociatedConformance(
GenericTypeParamType::getType(/*depth*/ 0, /*index*/ 0, C),
equatableProtocol);
auto equatable = hashable.getAssociatedConformance(C.TheSelfType, equatableProtocol);
assert(equatable.isAbstract() == hashable.isAbstract());
if (equatable.isConcrete())

View File

@@ -3458,8 +3458,7 @@ static Comparison compareDeclsForInference(DeclContext *DC, ValueDecl *decl1,
if (!sig1 || !sig2)
return TypeChecker::compareDeclarations(DC, decl1, decl2);
auto selfParam = GenericTypeParamType::getType(/*depth*/ 0, /*index*/ 0,
decl1->getASTContext());
auto selfParam = DC->getASTContext().TheSelfType;
// Collect the protocols required by extension 1.
Type class1;

View File

@@ -1077,11 +1077,8 @@ GetDistributedActorAsActorConformanceRequest::evaluate(
if (!ext)
return nullptr;
auto genericParam = GenericTypeParamType::getType(/*depth=*/0, /*index=*/0,
ctx);
auto distributedActorAsActorConformance = ctx.getNormalConformance(
Type(genericParam), actorProto, SourceLoc(), ext,
Type(ctx.TheSelfType), actorProto, SourceLoc(), ext,
ProtocolConformanceState::Incomplete, ProtocolConformanceOptions());
// NOTE: Normally we "register" a conformance, but here we don't
// because we cannot (currently) register them in a protocol,

View File

@@ -26,15 +26,14 @@ TEST(ASTDumper, ArchetypeType) {
TestContext C;
auto &ctx = C.Ctx;
auto *genericParamTy = GenericTypeParamType::getType(0, 0, ctx);
auto sig = buildGenericSignature(ctx, nullptr, {genericParamTy}, {},
auto sig = buildGenericSignature(ctx, nullptr, {ctx.TheSelfType}, {},
/*allowInverses=*/true);
TypeBase *archetype = nullptr;
{
llvm::SmallVector<ProtocolDecl *> protocols;
archetype = PrimaryArchetypeType::getNew(ctx, sig.getGenericEnvironment(),
genericParamTy, protocols, Type(),
ctx.TheSelfType, protocols, Type(),
nullptr);
}