Skip to content

Commit a510e9b

Browse files
committed
[GSB] Eliminate most uses of PotentialArchetype::getBuilder().
The remaining two uses after this refactor are... stubborn.
1 parent 42c1619 commit a510e9b

File tree

4 files changed

+41
-30
lines changed

4 files changed

+41
-30
lines changed

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ class GenericSignatureBuilder {
208208
///
209209
/// \returns true if this conformance is new to the equivalence class,
210210
/// and false otherwise.
211-
bool recordConformanceConstraint(ResolvedType type,
211+
bool recordConformanceConstraint(GenericSignatureBuilder &builder,
212+
ResolvedType type,
212213
ProtocolDecl *proto,
213214
FloatingRequirementSource source);
214215

@@ -236,12 +237,14 @@ class GenericSignatureBuilder {
236237
/// \param otherConcreteTypes If non-null, will be filled in the all of the
237238
/// concrete types we found (other than the result) with the same name.
238239
TypeDecl *lookupNestedType(
240+
GenericSignatureBuilder &builder,
239241
Identifier name,
240242
SmallVectorImpl<TypeDecl *> *otherConcreteTypes = nullptr);
241243

242244
/// Retrieve the "anchor" type that canonically describes this equivalence
243245
/// class, for use in the canonical type.
244-
Type getAnchor(ArrayRef<GenericTypeParamType *> genericParams);
246+
Type getAnchor(GenericSignatureBuilder &builder,
247+
ArrayRef<GenericTypeParamType *> genericParams);
245248

246249
/// \brief Retrieve (or build) the contextual type corresponding to
247250
/// this equivalence class within the given generic environment.
@@ -1701,6 +1704,7 @@ class GenericSignatureBuilder::PotentialArchetype {
17011704
/// type or typealias of the given protocol, unless the \c kind implies that
17021705
/// a potential archetype should not be created if it's missing.
17031706
PotentialArchetype *updateNestedTypeForConformance(
1707+
GenericSignatureBuilder &builder,
17041708
TypeDecl *type,
17051709
ArchetypeResolutionKind kind);
17061710

lib/AST/GenericSignature.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,8 @@ bool GenericSignature::isCanonicalTypeInContext(Type type,
754754
if (!equivClass) return false;
755755

756756
return (equivClass->concreteType ||
757-
!component->isEqual(equivClass->getAnchor(getGenericParams())));
757+
!component->isEqual(equivClass->getAnchor(builder,
758+
getGenericParams())));
758759
});
759760
}
760761

@@ -784,7 +785,7 @@ CanType GenericSignature::getCanonicalTypeInContext(Type type,
784785
return getCanonicalTypeInContext(equivClass->concreteType, builder);
785786
}
786787

787-
return equivClass->getAnchor(getGenericParams());
788+
return equivClass->getAnchor(builder, getGenericParams());
788789
});
789790

790791
auto result = type->getCanonicalType();

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,10 +1489,12 @@ std::string GenericSignatureBuilder::PotentialArchetype::getDebugName() const {
14891489

14901490
auto parent = getParent();
14911491
if (!parent) {
1492-
return GenericTypeParamType::get(getGenericParamKey().Depth,
1493-
getGenericParamKey().Index,
1494-
getBuilder()->getASTContext())->getName()
1495-
.str();
1492+
static const char *tau = u8"\u03C4_";
1493+
1494+
llvm::raw_svector_ostream os(result);
1495+
os << tau << getGenericParamKey().Depth << '_'
1496+
<< getGenericParamKey().Index;
1497+
return os.str().str();
14961498
}
14971499

14981500
// Nested types.
@@ -1616,11 +1618,10 @@ class GenericSignatureBuilder::ResolvedType {
16161618
};
16171619

16181620
bool EquivalenceClass::recordConformanceConstraint(
1621+
GenericSignatureBuilder &builder,
16191622
ResolvedType type,
16201623
ProtocolDecl *proto,
16211624
FloatingRequirementSource source) {
1622-
auto &builder = *members.front()->getBuilder();
1623-
16241625
// If we haven't seen a conformance to this protocol yet, add it.
16251626
bool inserted = false;
16261627
auto known = conformsTo.find(proto);
@@ -1641,7 +1642,7 @@ bool EquivalenceClass::recordConformanceConstraint(
16411642
// Resolve any associated type members.
16421643
for (auto assocType : proto->getAssociatedTypeMembers()) {
16431644
type.realizePotentialArchetype(builder)->updateNestedTypeForConformance(
1644-
assocType,
1645+
builder, assocType,
16451646
ArchetypeResolutionKind::AlreadyKnown);
16461647
}
16471648
}
@@ -1783,6 +1784,7 @@ static int compareAssociatedTypes(AssociatedTypeDecl *assocType1,
17831784
}
17841785

17851786
TypeDecl *EquivalenceClass::lookupNestedType(
1787+
GenericSignatureBuilder &builder,
17861788
Identifier name,
17871789
SmallVectorImpl<TypeDecl *> *otherConcreteTypes) {
17881790
// Populates the result structures from the given cache entry.
@@ -1892,8 +1894,7 @@ TypeDecl *EquivalenceClass::lookupNestedType(
18921894

18931895
// Infer same-type constraints among same-named associated type anchors.
18941896
if (assocTypeAnchors.size() > 1) {
1895-
auto &builder = *members.front()->getBuilder();
1896-
auto anchorType = getAnchor({ });
1897+
auto anchorType = getAnchor(builder, { });
18971898
auto inferredSource = FloatingRequirementSource::forInferred(nullptr);
18981899
for (auto assocType : assocTypeAnchors) {
18991900
if (assocType == bestAssocType) continue;
@@ -1937,9 +1938,9 @@ TypeDecl *EquivalenceClass::lookupNestedType(
19371938
}
19381939

19391940
Type EquivalenceClass::getAnchor(
1941+
GenericSignatureBuilder &builder,
19401942
ArrayRef<GenericTypeParamType *> genericParams) {
1941-
auto anchorPA =
1942-
members.front()->getArchetypeAnchor(*members.front()->getBuilder());
1943+
auto anchorPA = members.front()->getArchetypeAnchor(builder);
19431944
return anchorPA->getDependentType(genericParams);
19441945
}
19451946

@@ -1949,7 +1950,7 @@ Type EquivalenceClass::getTypeInContext(GenericSignatureBuilder &builder,
19491950
genericEnv->getGenericParams();
19501951

19511952
// The anchor descr
1952-
Type anchor = getAnchor(genericParams);
1953+
Type anchor = getAnchor(builder, genericParams);
19531954

19541955
// If this equivalence class is mapped to a concrete type, produce that
19551956
// type.
@@ -2271,7 +2272,7 @@ GenericSignatureBuilder::resolveConcreteConformance(ResolvedType type,
22712272
}
22722273

22732274
concreteSource = concreteSource->viaConcrete(*this, *conformance);
2274-
equivClass->recordConformanceConstraint(type, proto, concreteSource);
2275+
equivClass->recordConformanceConstraint(*this, type, proto, concreteSource);
22752276
addConditionalRequirements(*this, *conformance);
22762277
return concreteSource;
22772278
}
@@ -2302,7 +2303,7 @@ const RequirementSource *GenericSignatureBuilder::resolveSuperConformance(
23022303

23032304
superclassSource =
23042305
superclassSource->viaSuperclass(*this, *conformance);
2305-
equivClass->recordConformanceConstraint(type, proto, superclassSource);
2306+
equivClass->recordConformanceConstraint(*this, type, proto, superclassSource);
23062307
addConditionalRequirements(*this, *conformance);
23072308
return superclassSource;
23082309
}
@@ -2653,7 +2654,7 @@ static void concretizeNestedTypeFromConcreteParent(
26532654
// add it now; it was elided earlier.
26542655
if (parentEquiv->conformsTo.count(proto) == 0) {
26552656
auto source = parentEquiv->concreteTypeConstraints.front().source;
2656-
parentEquiv->recordConformanceConstraint(parent, proto, source);
2657+
parentEquiv->recordConformanceConstraint(builder, parent, proto, source);
26572658
}
26582659

26592660
assert(parentEquiv->conformsTo.count(proto) > 0 &&
@@ -2695,18 +2696,19 @@ PotentialArchetype *PotentialArchetype::getNestedArchetypeAnchor(
26952696
ArchetypeResolutionKind kind) {
26962697
SmallVector<TypeDecl *, 4> concreteDecls;
26972698
auto bestType =
2698-
getOrCreateEquivalenceClass()->lookupNestedType(name, &concreteDecls);
2699+
getOrCreateEquivalenceClass()->lookupNestedType(builder, name,
2700+
&concreteDecls);
26992701

27002702
// We didn't find any type with this name.
27012703
if (!bestType) return nullptr;
27022704

27032705
// Resolve the nested type.
2704-
auto resultPA = updateNestedTypeForConformance(bestType, kind);
2706+
auto resultPA = updateNestedTypeForConformance(builder, bestType, kind);
27052707

27062708
// Update for all of the concrete decls with this name, which will introduce
27072709
// various same-type constraints.
27082710
for (auto concreteDecl : concreteDecls) {
2709-
(void)updateNestedTypeForConformance(concreteDecl,
2711+
(void)updateNestedTypeForConformance(builder, concreteDecl,
27102712
ArchetypeResolutionKind::WellFormed);
27112713
}
27122714

@@ -2715,6 +2717,7 @@ PotentialArchetype *PotentialArchetype::getNestedArchetypeAnchor(
27152717

27162718

27172719
PotentialArchetype *PotentialArchetype::updateNestedTypeForConformance(
2720+
GenericSignatureBuilder &builder,
27182721
TypeDecl *type,
27192722
ArchetypeResolutionKind kind) {
27202723
if (!type) return nullptr;
@@ -2731,7 +2734,7 @@ PotentialArchetype *PotentialArchetype::updateNestedTypeForConformance(
27312734
// process delayed requirements if anything changed.
27322735
SWIFT_DEFER {
27332736
if (kind == ArchetypeResolutionKind::CompleteWellFormed)
2734-
getBuilder()->processDelayedRequirements();
2737+
builder.processDelayedRequirements();
27352738
};
27362739

27372740
Identifier name = assocType ? assocType->getName() : concreteDecl->getName();
@@ -2746,7 +2749,6 @@ PotentialArchetype *PotentialArchetype::updateNestedTypeForConformance(
27462749
PotentialArchetype *resultPA = nullptr;
27472750
auto knownNestedTypes = NestedTypes.find(name);
27482751
bool shouldUpdatePA = false;
2749-
auto &builder = *getBuilder();
27502752
if (knownNestedTypes != NestedTypes.end()) {
27512753
for (auto existingPA : knownNestedTypes->second) {
27522754
// Do we have an associated-type match?
@@ -3091,7 +3093,8 @@ static Type resolveDependentMemberTypes(GenericSignatureBuilder &builder,
30913093
if (!parentEquivClass)
30923094
return ErrorType::get(depTy);
30933095

3094-
auto memberType = parentEquivClass->lookupNestedType(depTy->getName());
3096+
auto memberType =
3097+
parentEquivClass->lookupNestedType(builder, depTy->getName());
30953098
if (!memberType)
30963099
return ErrorType::get(depTy);
30973100

@@ -3137,7 +3140,8 @@ ResolvedType GenericSignatureBuilder::maybeResolveEquivalenceClass(
31373140
if (auto assocType = depMemTy->getAssocType()) {
31383141
nestedTypeDecl = assocType;
31393142
} else {
3140-
nestedTypeDecl = baseEquivClass->lookupNestedType(depMemTy->getName());
3143+
nestedTypeDecl =
3144+
baseEquivClass->lookupNestedType(*this, depMemTy->getName());
31413145
if (!nestedTypeDecl) {
31423146
return ResolvedType::forUnresolved(baseEquivClass);
31433147
}
@@ -3155,7 +3159,8 @@ ResolvedType GenericSignatureBuilder::maybeResolveEquivalenceClass(
31553159
}
31563160

31573161
auto nestedPA =
3158-
basePA->updateNestedTypeForConformance(nestedTypeDecl, resolutionKind);
3162+
basePA->updateNestedTypeForConformance(*this, nestedTypeDecl,
3163+
resolutionKind);
31593164
if (!nestedPA)
31603165
return ResolvedType::forUnresolved(baseEquivClass);
31613166

@@ -3613,7 +3618,7 @@ ConstraintResult GenericSignatureBuilder::addConformanceRequirement(
36133618
// Add the conformance requirement, bailing out earlier if we've already
36143619
// seen it.
36153620
auto equivClass = type.getEquivalenceClass();
3616-
if (!equivClass->recordConformanceConstraint(type, proto, source))
3621+
if (!equivClass->recordConformanceConstraint(*this, type, proto, source))
36173622
return ConstraintResult::Resolved;
36183623

36193624
auto resolvedSource = source.getSource(*this, type.getDependentType());
@@ -3960,6 +3965,7 @@ void GenericSignatureBuilder::addedNestedType(PotentialArchetype *nestedPA) {
39603965

39613966
PotentialArchetype *existingPA =
39623967
parentRepPA->updateNestedTypeForConformance(
3968+
*this,
39633969
nestedPA->getResolvedType(),
39643970
ArchetypeResolutionKind::WellFormed);
39653971

@@ -4071,7 +4077,7 @@ GenericSignatureBuilder::addSameTypeRequirementBetweenArchetypes(
40714077
// Add all of the protocol conformance requirements of T2 to T1.
40724078
if (equivClass2) {
40734079
for (const auto &entry : equivClass2->conformsTo) {
4074-
equivClass->recordConformanceConstraint(T1, entry.first,
4080+
equivClass->recordConformanceConstraint(*this, T1, entry.first,
40754081
entry.second.front().source);
40764082

40774083
auto &constraints1 = equivClass->conformsTo[entry.first];

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Type CompleteGenericTypeResolver::resolveDependentMemberType(
142142

143143
// Look for a nested type with the given name.
144144
if (auto nestedType =
145-
baseEquivClass->lookupNestedType(ref->getIdentifier())) {
145+
baseEquivClass->lookupNestedType(builder, ref->getIdentifier())) {
146146
// Record the type we found.
147147
ref->setValue(nestedType, nullptr);
148148
} else {

0 commit comments

Comments
 (0)