Skip to content

Commit b434c9f

Browse files
authored
Merge pull request swiftlang#75984 from slavapestov/clean-up-opened-archetype
Clean up OpenedArchetypeType construction and more
2 parents 135ce46 + ff308e9 commit b434c9f

40 files changed

+245
-339
lines changed

include/swift/AST/LocalArchetypeRequirementCollector.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct MapLocalArchetypesOutOfContext {
5050
ArrayRef<GenericEnvironment *> capturedEnvs)
5151
: baseGenericSig(baseGenericSig), capturedEnvs(capturedEnvs) {}
5252

53+
Type getInterfaceType(Type interfaceTy, GenericEnvironment *genericEnv) const;
5354
Type operator()(SubstitutableType *type) const;
5455
};
5556

include/swift/AST/Types.h

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -921,10 +921,6 @@ class alignas(1 << TypeAlignInBits) TypeBase
921921
/// bound.
922922
bool isClassExistentialType();
923923

924-
/// Opens an existential instance or meta-type and returns the opened type.
925-
Type openAnyExistentialType(OpenedArchetypeType *&opened,
926-
GenericSignature parentSig);
927-
928924
/// Break an existential down into a set of constraints.
929925
ExistentialLayout getExistentialLayout();
930926

@@ -6821,27 +6817,21 @@ class OpenedArchetypeType final : public LocalArchetypeType,
68216817
/// of an existential value.
68226818
///
68236819
/// \param existential The existential type to open.
6824-
/// \param parentSig The generic signature of the context opening
6825-
/// this existential.
6826-
///
68276820
/// \param knownID When non-empty, the known ID of the archetype. When empty,
68286821
/// a fresh archetype with a unique ID will be opened.
68296822
static CanTypeWrapper<OpenedArchetypeType>
6830-
get(CanType existential, GenericSignature parentSig,
6831-
std::optional<UUID> knownID = std::nullopt);
6823+
get(CanType existential, std::optional<UUID> knownID = std::nullopt);
68326824

68336825
/// Get or create an archetype that represents the opened type
68346826
/// of an existential value.
68356827
///
68366828
/// \param existential The existential type to open.
68376829
/// \param interfaceType The interface type represented by this archetype.
6838-
/// \param parentSig The generic signature of the context opening
6839-
/// this existential.
68406830
///
68416831
/// \param knownID When non-empty, the known ID of the archetype. When empty,
68426832
/// a fresh archetype with a unique ID will be opened.
68436833
static CanTypeWrapper<OpenedArchetypeType>
6844-
get(CanType existential, Type interfaceType, GenericSignature parentSig,
6834+
get(CanType existential, Type interfaceType,
68456835
std::optional<UUID> knownID = std::nullopt);
68466836

68476837
/// Create a new archetype that represents the opened type
@@ -6853,10 +6843,7 @@ class OpenedArchetypeType final : public LocalArchetypeType,
68536843
///
68546844
/// \param existential The existential type or existential metatype to open.
68556845
/// \param interfaceType The interface type represented by this archetype.
6856-
/// \param parentSig The generic signature of the context opening
6857-
/// this existential.
6858-
static CanType getAny(CanType existential, Type interfaceType,
6859-
GenericSignature parentSig);
6846+
static Type getAny(Type existential, Type interfaceType);
68606847

68616848
/// Create a new archetype that represents the opened type
68626849
/// of an existential value.
@@ -6866,9 +6853,7 @@ class OpenedArchetypeType final : public LocalArchetypeType,
68666853
/// will unwrap any existential metatype containers.
68676854
///
68686855
/// \param existential The existential type or existential metatype to open.
6869-
/// \param parentSig The generic signature of the context opening
6870-
/// this existential.
6871-
static CanType getAny(CanType existential, GenericSignature parentSig);
6856+
static Type getAny(Type existential);
68726857

68736858
/// Retrieve the ID number of this opened existential.
68746859
UUID getOpenedExistentialID() const;

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,10 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
337337
void remapRootOpenedType(CanOpenedArchetypeType archetypeTy) {
338338
assert(archetypeTy->isRoot());
339339

340-
auto sig = Builder.getFunction().getGenericSignature();
341340
auto origExistentialTy = archetypeTy->getExistentialType()
342341
->getCanonicalType();
343342
auto substExistentialTy = getOpASTType(origExistentialTy);
344-
auto replacementTy = OpenedArchetypeType::get(substExistentialTy, sig);
343+
auto replacementTy = OpenedArchetypeType::get(substExistentialTy);
345344
registerLocalArchetypeRemapping(archetypeTy, replacementTy);
346345
}
347346

lib/AST/ASTContext.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5336,50 +5336,53 @@ CanTypeWrapper<OpenedArchetypeType> OpenedArchetypeType::getNew(
53365336
}
53375337

53385338
CanTypeWrapper<OpenedArchetypeType>
5339-
OpenedArchetypeType::get(CanType existential, GenericSignature parentSig,
5340-
std::optional<UUID> knownID) {
5339+
OpenedArchetypeType::get(CanType existential, std::optional<UUID> knownID) {
53415340
assert(existential->isExistentialType());
53425341
auto interfaceType = OpenedArchetypeType::getSelfInterfaceTypeFromContext(
5343-
parentSig, existential->getASTContext());
5344-
return get(existential, interfaceType, parentSig, knownID);
5342+
GenericSignature(), existential->getASTContext());
5343+
return get(existential, interfaceType, knownID);
53455344
}
53465345

53475346
CanOpenedArchetypeType OpenedArchetypeType::get(CanType existential,
53485347
Type interfaceType,
5349-
GenericSignature parentSig,
53505348
std::optional<UUID> knownID) {
5351-
assert(!interfaceType->hasArchetype() && "must be interface type");
5349+
assert(!existential->hasTypeParameter());
53525350

53535351
if (!knownID)
53545352
knownID = UUID::fromTime();
53555353

53565354
auto *genericEnv =
5357-
GenericEnvironment::forOpenedExistential(existential, parentSig, *knownID);
5355+
GenericEnvironment::forOpenedExistential(
5356+
existential, GenericSignature(), *knownID);
53585357

53595358
// Map the interface type into that environment.
53605359
auto result = genericEnv->mapTypeIntoContext(interfaceType)
53615360
->castTo<OpenedArchetypeType>();
53625361
return CanOpenedArchetypeType(result);
53635362
}
53645363

5365-
CanType OpenedArchetypeType::getAny(CanType existential, Type interfaceType,
5366-
GenericSignature parentSig) {
5364+
Type OpenedArchetypeType::getAny(Type existential, Type interfaceTy) {
53675365
assert(existential->isAnyExistentialType());
53685366
if (auto metatypeTy = existential->getAs<ExistentialMetatypeType>()) {
53695367
auto instanceTy =
53705368
metatypeTy->getExistentialInstanceType()->getCanonicalType();
5371-
return CanMetatypeType::get(
5372-
OpenedArchetypeType::getAny(instanceTy, interfaceType, parentSig));
5369+
auto openedInstanceTy = OpenedArchetypeType::getAny(
5370+
instanceTy, interfaceTy);
5371+
if (metatypeTy->hasRepresentation()) {
5372+
return MetatypeType::get(openedInstanceTy,
5373+
metatypeTy->getRepresentation());
5374+
}
5375+
return MetatypeType::get(openedInstanceTy);
53735376
}
53745377
assert(existential->isExistentialType());
5375-
return OpenedArchetypeType::get(existential, interfaceType, parentSig);
5378+
return OpenedArchetypeType::get(existential->getCanonicalType(),
5379+
interfaceTy);
53765380
}
53775381

5378-
CanType OpenedArchetypeType::getAny(CanType existential,
5379-
GenericSignature parentSig) {
5382+
Type OpenedArchetypeType::getAny(Type existential) {
53805383
auto interfaceTy = OpenedArchetypeType::getSelfInterfaceTypeFromContext(
5381-
parentSig, existential->getASTContext());
5382-
return getAny(existential, interfaceTy, parentSig);
5384+
GenericSignature(), existential->getASTContext());
5385+
return getAny(existential, interfaceTy);
53835386
}
53845387

53855388
void SubstitutionMap::Storage::Profile(

lib/AST/ASTMangler.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/ExistentialLayout.h"
2323
#include "swift/AST/Expr.h"
2424
#include "swift/AST/FileUnit.h"
25+
#include "swift/AST/GenericEnvironment.h"
2526
#include "swift/AST/GenericSignature.h"
2627
#include "swift/AST/Initializer.h"
2728
#include "swift/AST/LazyResolver.h"
@@ -2248,12 +2249,15 @@ void ASTMangler::appendOpaqueTypeArchetype(ArchetypeType *archetype,
22482249

22492250
appendOperator("Qo", Index(genericParam->getIndex()));
22502251
} else {
2252+
auto *env = archetype->getGenericEnvironment();
2253+
appendType(env->mapTypeIntoContext(interfaceType->getRootGenericParam()),
2254+
sig, forDecl);
2255+
22512256
// Mangle associated types of opaque archetypes like dependent member
22522257
// types, so that they can be accurately demangled at runtime.
2253-
appendType(Type(archetype->getRoot()), sig, forDecl);
22542258
bool isAssocTypeAtDepth = false;
22552259
appendAssocType(
2256-
archetype->getInterfaceType()->castTo<DependentMemberType>(),
2260+
interfaceType->castTo<DependentMemberType>(),
22572261
sig, isAssocTypeAtDepth);
22582262
appendOperator(isAssocTypeAtDepth ? "QX" : "Qx");
22592263
}

lib/AST/ASTPrinter.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7021,16 +7021,17 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
70217021
Printer << "each ";
70227022
}
70237023

7024-
void printArchetypeCommon(Type interfaceTy, ArchetypeType *archetypeTy) {
7024+
void printArchetypeCommon(Type interfaceTy, GenericEnvironment *env) {
70257025
if (auto *paramTy = interfaceTy->getAs<GenericTypeParamType>()) {
7026-
assert(archetypeTy->isRoot());
7027-
70287026
if (Options.AlternativeTypeNames) {
7029-
auto found = Options.AlternativeTypeNames->find(CanType(archetypeTy));
7030-
if (found != Options.AlternativeTypeNames->end()) {
7031-
if (paramTy->isParameterPack()) printEach();
7032-
Printer << found->second.str();
7033-
return;
7027+
auto archetypeTy = env->mapTypeIntoContext(paramTy)->getAs<GenericTypeParamType>();
7028+
if (archetypeTy) {
7029+
auto found = Options.AlternativeTypeNames->find(CanType(archetypeTy));
7030+
if (found != Options.AlternativeTypeNames->end()) {
7031+
if (paramTy->isParameterPack()) printEach();
7032+
Printer << found->second.str();
7033+
return;
7034+
}
70347035
}
70357036
}
70367037

@@ -7039,18 +7040,18 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
70397040
}
70407041

70417042
auto *memberTy = interfaceTy->castTo<DependentMemberType>();
7042-
if (memberTy->getBase()->is<GenericTypeParamType>())
7043-
visitParentType(archetypeTy->getRoot());
7043+
if (auto *paramTy = memberTy->getBase()->getAs<GenericTypeParamType>())
7044+
visitParentType(env->mapTypeIntoContext(paramTy));
70447045
else {
7045-
printArchetypeCommon(memberTy->getBase(), archetypeTy->getRoot());
7046+
printArchetypeCommon(memberTy->getBase(), env);
70467047
Printer << ".";
70477048
}
70487049

70497050
printDependentMember(memberTy);
70507051
}
70517052

70527053
void visitPrimaryArchetypeType(PrimaryArchetypeType *T) {
7053-
printArchetypeCommon(T->getInterfaceType(), T);
7054+
printArchetypeCommon(T->getInterfaceType(), T->getGenericEnvironment());
70547055
}
70557056

70567057
void visitOpaqueTypeArchetypeType(OpaqueTypeArchetypeType *T) {
@@ -7059,7 +7060,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
70597060

70607061
if (!paramTy) {
70617062
assert(interfaceTy->is<DependentMemberType>());
7062-
printArchetypeCommon(interfaceTy, T);
7063+
printArchetypeCommon(interfaceTy, T->getGenericEnvironment());
70637064
return;
70647065
}
70657066

@@ -7144,7 +7145,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
71447145
}
71457146

71467147
void visitPackArchetypeType(PackArchetypeType *T) {
7147-
printArchetypeCommon(T->getInterfaceType(), T);
7148+
printArchetypeCommon(T->getInterfaceType(), T->getGenericEnvironment());
71487149
}
71497150

71507151
void visitGenericTypeParamType(GenericTypeParamType *T) {

lib/AST/GenericEnvironment.cpp

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -149,53 +149,6 @@ UUID GenericEnvironment::getOpenedElementUUID() const {
149149
return getTrailingObjects<OpenedElementEnvironmentData>()->uuid;
150150
}
151151

152-
namespace {
153-
154-
struct FindOpenedElementParam {
155-
ArrayRef<Type> openedPacks;
156-
ArrayRef<GenericTypeParamType *> packElementParams;
157-
158-
FindOpenedElementParam(const GenericEnvironment *env,
159-
ArrayRef<Type> openedPacks)
160-
: openedPacks(openedPacks),
161-
packElementParams(
162-
env->getGenericSignature().getInnermostGenericParams()) {
163-
assert(openedPacks.size() == packElementParams.size());
164-
}
165-
166-
GenericTypeParamType *operator()(Type packParam) {
167-
for (auto i : indices(openedPacks)) {
168-
if (openedPacks[i]->isEqual(packParam))
169-
return packElementParams[i];
170-
}
171-
llvm_unreachable("parameter was not an opened pack parameter");
172-
}
173-
};
174-
175-
struct FindElementArchetypeForOpenedPackParam {
176-
FindOpenedElementParam findElementParam;
177-
QueryInterfaceTypeSubstitutions getElementArchetype;
178-
179-
FindElementArchetypeForOpenedPackParam(const GenericEnvironment *env,
180-
ArrayRef<Type> openedPacks)
181-
: findElementParam(env, openedPacks), getElementArchetype(env) {}
182-
183-
184-
Type operator()(Type interfaceType) {
185-
assert(interfaceType->isTypeParameter());
186-
if (auto member = interfaceType->getAs<DependentMemberType>()) {
187-
auto baseArchetype = (*this)(member->getBase())
188-
->castTo<ElementArchetypeType>();
189-
return baseArchetype->getNestedType(member->getAssocType())
190-
->castTo<ElementArchetypeType>();
191-
}
192-
assert(interfaceType->is<GenericTypeParamType>());
193-
return getElementArchetype(findElementParam(interfaceType));
194-
}
195-
};
196-
197-
}
198-
199152
void GenericEnvironment::forEachPackElementArchetype(
200153
llvm::function_ref<void(ElementArchetypeType *)> function) const {
201154
auto packElements = getGenericSignature().getInnermostGenericParams();
@@ -652,6 +605,41 @@ Type GenericEnvironment::mapTypeIntoContext(GenericTypeParamType *type) const {
652605
return result;
653606
}
654607

608+
namespace {
609+
610+
struct FindElementArchetypeForOpenedPackParam {
611+
ArrayRef<Type> openedPacks;
612+
ArrayRef<GenericTypeParamType *> packElementParams;
613+
const GenericEnvironment *env;
614+
615+
FindElementArchetypeForOpenedPackParam(const GenericEnvironment *env,
616+
ArrayRef<Type> openedPacks)
617+
: openedPacks(openedPacks),
618+
packElementParams(env->getGenericSignature().getInnermostGenericParams()),
619+
env(env) {}
620+
621+
Type getInterfaceType(Type interfaceType) const {
622+
if (auto member = interfaceType->getAs<DependentMemberType>()) {
623+
return DependentMemberType::get(getInterfaceType(member->getBase()),
624+
member->getAssocType());
625+
}
626+
627+
assert(interfaceType->is<GenericTypeParamType>());
628+
for (auto i : indices(openedPacks)) {
629+
if (openedPacks[i]->isEqual(interfaceType))
630+
return packElementParams[i];
631+
}
632+
633+
llvm_unreachable("parameter was not an opened pack parameter");
634+
}
635+
636+
Type operator()(Type interfaceType) const {
637+
return env->mapTypeIntoContext(getInterfaceType(interfaceType));
638+
}
639+
};
640+
641+
}
642+
655643
/// So this expects a type written with the archetypes of the original generic
656644
/// environment, not 'this', the opened element environment, because it is the
657645
/// original PackArchetypes that become ElementArchetypes. Also this function

0 commit comments

Comments
 (0)