Skip to content

Commit 52ce02e

Browse files
committed
AST: Remove ArchetypeType::getRoot()
1 parent 21905af commit 52ce02e

File tree

5 files changed

+40
-56
lines changed

5 files changed

+40
-56
lines changed

include/swift/AST/Types.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6577,10 +6577,6 @@ class ArchetypeType : public SubstitutableType,
65776577
/// which the archetype conforms.
65786578
Type getNestedTypeByName(Identifier name);
65796579

6580-
/// Return the archetype that represents the root generic parameter of its
6581-
/// interface type.
6582-
ArchetypeType *getRoot() const;
6583-
65846580
/// Determine whether this is a root archetype within the environment.
65856581
bool isRoot() const;
65866582

@@ -6761,19 +6757,12 @@ class LocalArchetypeType : public ArchetypeType {
67616757
using ArchetypeType::ArchetypeType;
67626758

67636759
public:
6764-
LocalArchetypeType *getRoot() const {
6765-
return cast<LocalArchetypeType>(ArchetypeType::getRoot());
6766-
}
6767-
67686760
static bool classof(const TypeBase *type) {
67696761
return type->getKind() == TypeKind::OpenedArchetype ||
67706762
type->getKind() == TypeKind::ElementArchetype;
67716763
}
67726764
};
67736765
BEGIN_CAN_TYPE_WRAPPER(LocalArchetypeType, ArchetypeType)
6774-
CanLocalArchetypeType getRoot() const {
6775-
return CanLocalArchetypeType(getPointer()->getRoot());
6776-
}
67776766
END_CAN_TYPE_WRAPPER(LocalArchetypeType, ArchetypeType)
67786767

67796768
/// An archetype that represents the dynamic type of an opened existential.
@@ -6828,12 +6817,6 @@ class OpenedArchetypeType final : public LocalArchetypeType,
68286817
/// Retrieve the ID number of this opened existential.
68296818
UUID getOpenedExistentialID() const;
68306819

6831-
/// Return the archetype that represents the root generic parameter of its
6832-
/// interface type.
6833-
OpenedArchetypeType *getRoot() const {
6834-
return cast<OpenedArchetypeType>(ArchetypeType::getRoot());
6835-
}
6836-
68376820
static bool classof(const TypeBase *T) {
68386821
return T->getKind() == TypeKind::OpenedArchetype;
68396822
}
@@ -6846,9 +6829,6 @@ class OpenedArchetypeType final : public LocalArchetypeType,
68466829
RecursiveTypeProperties properties);
68476830
};
68486831
BEGIN_CAN_TYPE_WRAPPER(OpenedArchetypeType, LocalArchetypeType)
6849-
CanOpenedArchetypeType getRoot() const {
6850-
return CanOpenedArchetypeType(getPointer()->getRoot());
6851-
}
68526832
END_CAN_TYPE_WRAPPER(OpenedArchetypeType, LocalArchetypeType)
68536833

68546834
/// A wrapper around a shape type to use in ArchetypeTrailingObjects
@@ -6916,12 +6896,6 @@ class ElementArchetypeType final : public LocalArchetypeType,
69166896
/// Retrieve the ID number of this opened element.
69176897
UUID getOpenedElementID() const;
69186898

6919-
/// Return the archetype that represents the root generic parameter of its
6920-
/// interface type.
6921-
ElementArchetypeType *getRoot() const {
6922-
return cast<ElementArchetypeType>(ArchetypeType::getRoot());
6923-
}
6924-
69256899
static bool classof(const TypeBase *T) {
69266900
return T->getKind() == TypeKind::ElementArchetype;
69276901
}
@@ -6933,9 +6907,6 @@ class ElementArchetypeType final : public LocalArchetypeType,
69336907
LayoutConstraint layout);
69346908
};
69356909
BEGIN_CAN_TYPE_WRAPPER(ElementArchetypeType, LocalArchetypeType)
6936-
CanElementArchetypeType getRoot() const {
6937-
return CanElementArchetypeType(getPointer()->getRoot());
6938-
}
69396910
END_CAN_TYPE_WRAPPER(ElementArchetypeType, LocalArchetypeType)
69406911

69416912
template<typename Type>

include/swift/SIL/SILModule.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,18 @@ class SILModule {
460460
SILValue getLocalGenericEnvironmentDef(GenericEnvironment *genericEnv,
461461
SILFunction *inFunction);
462462

463+
/// Returns the instruction which defines the given local generic environment,
464+
/// e.g. an open_existential_addr.
465+
///
466+
/// In contrast to getLocalGenericEnvironmentDef, it is required that all local
467+
/// generic environments are resolved.
468+
SingleValueInstruction *
469+
getLocalGenericEnvironmentDefInst(GenericEnvironment *genericEnv,
470+
SILFunction *inFunction) {
471+
return dyn_cast<SingleValueInstruction>(
472+
getLocalGenericEnvironmentDef(genericEnv, inFunction));
473+
}
474+
463475
/// Returns the instruction which defines the given root local archetype,
464476
/// e.g. an open_existential_addr.
465477
///

lib/AST/ParameterPack.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "swift/AST/ASTContext.h"
1919
#include "swift/AST/Decl.h"
20+
#include "swift/AST/GenericEnvironment.h"
2021
#include "swift/AST/GenericParamList.h"
2122
#include "swift/AST/ParameterList.h"
2223
#include "swift/AST/Type.h"
@@ -161,7 +162,14 @@ void TypeBase::getTypeParameterPacks(
161162
if (paramTy->isParameterPack())
162163
rootParameterPacks.push_back(paramTy);
163164
} else if (auto *archetypeTy = t->getAs<PackArchetypeType>()) {
164-
rootParameterPacks.push_back(archetypeTy->getRoot());
165+
if (archetypeTy->isRoot()) {
166+
rootParameterPacks.push_back(archetypeTy);
167+
} else {
168+
auto *genericEnv = archetypeTy->getGenericEnvironment();
169+
auto paramTy = archetypeTy->getInterfaceType()->getRootGenericParam();
170+
rootParameterPacks.push_back(
171+
genericEnv->mapTypeIntoContext(paramTy));
172+
}
165173
}
166174

167175
return false;

lib/AST/Type.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,17 +3378,6 @@ ArchetypeType::ArchetypeType(TypeKind Kind,
33783378
getSubclassTrailingObjects<ProtocolDecl *>());
33793379
}
33803380

3381-
ArchetypeType *ArchetypeType::getRoot() const {
3382-
if (isRoot()) {
3383-
return const_cast<ArchetypeType *>(this);
3384-
}
3385-
3386-
auto gp = InterfaceType->getRootGenericParam();
3387-
assert(gp && "Missing root generic parameter?");
3388-
return getGenericEnvironment()->mapTypeIntoContext(
3389-
Type(gp))->castTo<ArchetypeType>();
3390-
}
3391-
33923381
bool ArchetypeType::isRoot() const {
33933382
return getInterfaceType()->is<GenericTypeParamType>();
33943383
}

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
17441744
"Caller's generic param list.");
17451745
if (auto localA = getLocalArchetypeOf(A)) {
17461746
auto *openingInst =
1747-
F->getModule().getRootLocalArchetypeDefInst(localA.getRoot(), F);
1747+
F->getModule().getLocalGenericEnvironmentDefInst(
1748+
localA->getGenericEnvironment(), F);
17481749
require(I == nullptr || openingInst == I ||
17491750
properlyDominates(openingInst, I),
17501751
"Use of a local archetype should be dominated by a "
@@ -1893,12 +1894,10 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
18931894
require(isArchetypeValidInFunction(A, AI->getFunction()),
18941895
"Archetype to be substituted must be valid in function.");
18951896

1896-
const auto root = A.getRoot();
1897-
18981897
// Check that opened archetypes are properly tracked inside
18991898
// the current function.
1900-
auto *openingInst = F.getModule().getRootLocalArchetypeDefInst(
1901-
root, AI->getFunction());
1899+
auto *openingInst = F.getModule().getLocalGenericEnvironmentDefInst(
1900+
A->getGenericEnvironment(), AI->getFunction());
19021901
require(openingInst,
19031902
"Root opened archetype should be registered in SILModule");
19041903
require(openingInst == AI || properlyDominates(openingInst, AI),
@@ -4853,8 +4852,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
48534852
Ty.visit([&](CanType t) {
48544853
SILValue Def;
48554854
if (const auto archetypeTy = dyn_cast<LocalArchetypeType>(t)) {
4856-
Def = I->getModule().getRootLocalArchetypeDefInst(
4857-
archetypeTy.getRoot(), I->getFunction());
4855+
Def = I->getModule().getLocalGenericEnvironmentDefInst(
4856+
archetypeTy->getGenericEnvironment(), I->getFunction());
48584857
require(Def, "Root opened archetype should be registered in SILModule");
48594858
} else if (t->hasDynamicSelfType()) {
48604859
require(I->getFunction()->hasSelfParam() ||
@@ -6162,28 +6161,29 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
61626161
llvm::DenseMap<CanType, CanPackType>
61636162
collectOpenedElementArchetypeBindings(CanType type,
61646163
AnyPackIndexInst *indexedBy) {
6164+
llvm::DenseSet<GenericEnvironment *> visited;
61656165
llvm::DenseMap<CanType, CanPackType> result;
61666166

61676167
type.visit([&](CanType type) {
61686168
auto opened = dyn_cast<ElementArchetypeType>(type);
61696169
if (!opened) return;
6170-
opened = opened.getRoot();
6170+
6171+
auto *genericEnv = opened->getGenericEnvironment();
61716172

61726173
// Don't repeat this work if the same archetype is named twice.
6173-
if (result.count(opened)) return;
6174+
if (!visited.insert(genericEnv).second) return;
61746175

61756176
// Ignore archetypes defined by open_pack_elements not based on the
61766177
// same pack_index instruction.
61776178
auto openingInst =
6178-
F.getModule().getRootLocalArchetypeDef(opened,
6179+
F.getModule().getLocalGenericEnvironmentDef(genericEnv,
61796180
const_cast<SILFunction*>(&F));
61806181
auto opi = dyn_cast<OpenPackElementInst>(openingInst);
61816182
if (!opi || opi->getIndexOperand() != indexedBy) return;
61826183

61836184
// Map each root opened element archetype to its pack substitution.
61846185
// FIXME: remember conformances?
6185-
auto openedEnv = opi->getOpenedGenericEnvironment();
6186-
openedEnv->forEachPackElementBinding(
6186+
genericEnv->forEachPackElementBinding(
61876187
[&](ElementArchetypeType *elementArchetype, PackType *substitution) {
61886188
auto subPack = cast<PackType>(substitution->getCanonicalType());
61896189
result.insert({elementArchetype->getCanonicalType(), subPack});
@@ -6282,7 +6282,11 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
62826282
if (!archetype)
62836283
return type;
62846284

6285-
auto root = archetype->getRoot();
6285+
auto *genericEnv = archetype->getGenericEnvironment();
6286+
auto interfaceTy = archetype->getInterfaceType();
6287+
6288+
auto root = genericEnv->mapTypeIntoContext(
6289+
interfaceTy->getRootGenericParam())->castTo<ElementArchetypeType>();
62866290
auto it = allOpened.find(root->getCanonicalType());
62876291
assert(it != allOpened.end());
62886292

@@ -6295,10 +6299,10 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
62956299
assert(!indexedShape && "pack substitution doesn't match in shape");
62966300
}
62976301

6298-
if (archetype->isRoot())
6302+
if (interfaceTy->is<GenericTypeParamType>())
62996303
return packElementType;
63006304

6301-
return archetype->getInterfaceType()->castTo<DependentMemberType>()
6305+
return interfaceTy->castTo<DependentMemberType>()
63026306
->substRootParam(packElementType, LookUpConformanceInModule(),
63036307
std::nullopt);
63046308
};

0 commit comments

Comments
 (0)