@@ -1594,12 +1594,14 @@ class GenericSignatureBuilder::ResolvedType {
1594
1594
}
1595
1595
1596
1596
// / Retrieve the equivalence class into which a resolved type refers.
1597
- EquivalenceClass *getEquivalenceClass () const {
1597
+ EquivalenceClass *getEquivalenceClass (
1598
+ GenericSignatureBuilder &builder) const {
1598
1599
assert (*this && " Only for resolved types" );
1599
1600
if (equivClass) return equivClass;
1600
1601
1601
1602
// Create the equivalence class now.
1602
- return type.get <PotentialArchetype *>()->getOrCreateEquivalenceClass ();
1603
+ return type.get <PotentialArchetype *>()
1604
+ ->getOrCreateEquivalenceClass (builder);
1603
1605
}
1604
1606
1605
1607
// / Retrieve the unresolved result.
@@ -1655,6 +1657,23 @@ bool EquivalenceClass::recordConformanceConstraint(
1655
1657
return inserted;
1656
1658
}
1657
1659
1660
+ bool EquivalenceClass::recordSameTypeConstraint (
1661
+ GenericSignatureBuilder &builder,
1662
+ PotentialArchetype *type1,
1663
+ PotentialArchetype *type2,
1664
+ const RequirementSource *source) {
1665
+ sameTypeConstraints[type1].push_back ({type1, type2, source});
1666
+ ++NumSameTypeConstraints;
1667
+
1668
+ if (type1 != type2) {
1669
+ type2->getOrCreateEquivalenceClass (builder)
1670
+ ->sameTypeConstraints [type2].push_back ({type2, type1, source});
1671
+ ++NumSameTypeConstraints;
1672
+ }
1673
+
1674
+ return true ;
1675
+ }
1676
+
1658
1677
template <typename T>
1659
1678
Type Constraint<T>::getSubjectDependentType() const {
1660
1679
if (auto type = subject.dyn_cast <Type>())
@@ -2238,7 +2257,7 @@ static void addConditionalRequirements(GenericSignatureBuilder &builder,
2238
2257
const RequirementSource *
2239
2258
GenericSignatureBuilder::resolveConcreteConformance (ResolvedType type,
2240
2259
ProtocolDecl *proto) {
2241
- auto equivClass = type.getEquivalenceClass ();
2260
+ auto equivClass = type.getEquivalenceClass (* this );
2242
2261
auto concrete = equivClass->concreteType ;
2243
2262
if (!concrete) return nullptr ;
2244
2263
@@ -2280,7 +2299,7 @@ const RequirementSource *GenericSignatureBuilder::resolveSuperConformance(
2280
2299
ResolvedType type,
2281
2300
ProtocolDecl *proto) {
2282
2301
// Get the superclass constraint.
2283
- auto equivClass = type.getEquivalenceClass ();
2302
+ auto equivClass = type.getEquivalenceClass (* this );
2284
2303
Type superclass = equivClass->superclass ;
2285
2304
if (!superclass) return nullptr ;
2286
2305
@@ -2375,20 +2394,21 @@ static void maybeAddSameTypeRequirementForNestedType(
2375
2394
GenericSignatureBuilder::UnresolvedHandlingKind::GenerateConstraints);
2376
2395
}
2377
2396
2378
- auto PotentialArchetype::getOrCreateEquivalenceClass () const
2397
+ auto PotentialArchetype::getOrCreateEquivalenceClass (
2398
+ GenericSignatureBuilder &builder) const
2379
2399
-> EquivalenceClass * {
2380
2400
// The equivalence class is stored on the representative.
2381
2401
auto representative = getRepresentative ();
2382
2402
if (representative != this )
2383
- return representative->getOrCreateEquivalenceClass ();
2403
+ return representative->getOrCreateEquivalenceClass (builder );
2384
2404
2385
2405
// If we already have an equivalence class, return it.
2386
2406
if (auto equivClass = getEquivalenceClassIfPresent ())
2387
2407
return equivClass;
2388
2408
2389
2409
// Create a new equivalence class.
2390
2410
auto equivClass =
2391
- getBuilder ()-> Impl ->allocateEquivalenceClass (
2411
+ builder. Impl ->allocateEquivalenceClass (
2392
2412
const_cast <PotentialArchetype *>(this ));
2393
2413
representativeOrEquivClass = equivClass;
2394
2414
return equivClass;
@@ -2696,8 +2716,8 @@ PotentialArchetype *PotentialArchetype::getNestedArchetypeAnchor(
2696
2716
ArchetypeResolutionKind kind) {
2697
2717
SmallVector<TypeDecl *, 4 > concreteDecls;
2698
2718
auto bestType =
2699
- getOrCreateEquivalenceClass ()->lookupNestedType (builder, name,
2700
- &concreteDecls);
2719
+ getOrCreateEquivalenceClass (builder )->lookupNestedType (builder, name,
2720
+ &concreteDecls);
2701
2721
2702
2722
// We didn't find any type with this name.
2703
2723
if (!bestType) return nullptr ;
@@ -2772,7 +2792,7 @@ PotentialArchetype *PotentialArchetype::updateNestedTypeForConformance(
2772
2792
case ArchetypeResolutionKind::WellFormed: {
2773
2793
// Creating a new potential archetype in an equivalence class is a
2774
2794
// modification.
2775
- getOrCreateEquivalenceClass ()->modified (builder);
2795
+ getOrCreateEquivalenceClass (builder )->modified (builder);
2776
2796
2777
2797
void *mem = builder.Impl ->Allocator .Allocate <PotentialArchetype>();
2778
2798
if (assocType)
@@ -3135,7 +3155,7 @@ ResolvedType GenericSignatureBuilder::maybeResolveEquivalenceClass(
3135
3155
if (!resolvedBase) return resolvedBase;
3136
3156
3137
3157
// Find the nested type declaration for this.
3138
- auto baseEquivClass = resolvedBase.getEquivalenceClass ();
3158
+ auto baseEquivClass = resolvedBase.getEquivalenceClass (* this );
3139
3159
TypeDecl *nestedTypeDecl;
3140
3160
if (auto assocType = depMemTy->getAssocType ()) {
3141
3161
nestedTypeDecl = assocType;
@@ -3183,7 +3203,7 @@ ResolvedType GenericSignatureBuilder::maybeResolveEquivalenceClass(
3183
3203
}
3184
3204
3185
3205
return ResolvedType (resolvedMemberType,
3186
- nestedPA->getOrCreateEquivalenceClass ());
3206
+ nestedPA->getOrCreateEquivalenceClass (* this ));
3187
3207
}
3188
3208
3189
3209
// If it's not a type parameter, it won't directly resolve to one.
@@ -3208,7 +3228,7 @@ EquivalenceClass *GenericSignatureBuilder::resolveEquivalenceClass(
3208
3228
if (auto resolved =
3209
3229
maybeResolveEquivalenceClass (type, resolutionKind,
3210
3230
/* wantExactPotentialArchetype=*/ false ))
3211
- return resolved.getEquivalenceClass ();
3231
+ return resolved.getEquivalenceClass (* this );
3212
3232
3213
3233
return nullptr ;
3214
3234
}
@@ -3617,7 +3637,7 @@ ConstraintResult GenericSignatureBuilder::addConformanceRequirement(
3617
3637
FloatingRequirementSource source) {
3618
3638
// Add the conformance requirement, bailing out earlier if we've already
3619
3639
// seen it.
3620
- auto equivClass = type.getEquivalenceClass ();
3640
+ auto equivClass = type.getEquivalenceClass (* this );
3621
3641
if (!equivClass->recordConformanceConstraint (*this , type, proto, source))
3622
3642
return ConstraintResult::Resolved;
3623
3643
@@ -3630,7 +3650,7 @@ ConstraintResult GenericSignatureBuilder::addLayoutRequirementDirect(
3630
3650
ResolvedType type,
3631
3651
LayoutConstraint layout,
3632
3652
FloatingRequirementSource source) {
3633
- auto equivClass = type.getEquivalenceClass ();
3653
+ auto equivClass = type.getEquivalenceClass (* this );
3634
3654
3635
3655
// Update the layout in the equivalence class, if we didn't have one already.
3636
3656
bool anyChanges = false ;
@@ -3697,7 +3717,7 @@ bool GenericSignatureBuilder::updateSuperclass(
3697
3717
ResolvedType type,
3698
3718
Type superclass,
3699
3719
FloatingRequirementSource source) {
3700
- auto equivClass = type.getEquivalenceClass ();
3720
+ auto equivClass = type.getEquivalenceClass (* this );
3701
3721
3702
3722
// Local function to handle the update of superclass conformances
3703
3723
// when the superclass constraint changes.
@@ -3776,7 +3796,7 @@ ConstraintResult GenericSignatureBuilder::addSuperclassRequirementDirect(
3776
3796
auto resolvedSource = source.getSource (*this , type.getDependentType ());
3777
3797
3778
3798
// Record the constraint.
3779
- auto equivClass = type.getEquivalenceClass ();
3799
+ auto equivClass = type.getEquivalenceClass (* this );
3780
3800
equivClass->superclassConstraints .push_back (
3781
3801
ConcreteConstraint{type.getUnresolvedType (), superclass, resolvedSource});
3782
3802
equivClass->modified (*this );
@@ -3924,22 +3944,6 @@ ConstraintResult GenericSignatureBuilder::addTypeRequirement(
3924
3944
source);
3925
3945
}
3926
3946
3927
- void GenericSignatureBuilder::PotentialArchetype::addSameTypeConstraint (
3928
- PotentialArchetype *otherPA,
3929
- const RequirementSource *source) {
3930
- // Update the same-type constraints of this PA to reference the other PA.
3931
- getOrCreateEquivalenceClass ()->sameTypeConstraints [this ]
3932
- .push_back ({this , otherPA, source});
3933
- ++NumSameTypeConstraints;
3934
-
3935
- if (this != otherPA) {
3936
- // Update the same-type constraints of the other PA to reference this PA.
3937
- otherPA->getOrCreateEquivalenceClass ()->sameTypeConstraints [otherPA]
3938
- .push_back ({otherPA, this , source});
3939
- ++NumSameTypeConstraints;
3940
- }
3941
- }
3942
-
3943
3947
void GenericSignatureBuilder::addedNestedType (PotentialArchetype *nestedPA) {
3944
3948
// If there was already another type with this name within the parent
3945
3949
// potential archetype, equate this type with that one.
@@ -3982,16 +3986,16 @@ GenericSignatureBuilder::addSameTypeRequirementBetweenArchetypes(
3982
3986
PotentialArchetype *OrigT2,
3983
3987
const RequirementSource *Source)
3984
3988
{
3985
- // Record the same-type constraint.
3986
- OrigT1->addSameTypeConstraint (OrigT2, Source);
3987
-
3988
3989
// Operate on the representatives
3989
3990
auto T1 = OrigT1->getRepresentative ();
3990
3991
auto T2 = OrigT2->getRepresentative ();
3991
3992
3992
3993
// If the representatives are already the same, we're done.
3993
- if (T1 == T2)
3994
+ if (T1 == T2) {
3995
+ T1->getOrCreateEquivalenceClass (*this )
3996
+ ->recordSameTypeConstraint (*this , OrigT1, OrigT2, Source);
3994
3997
return ConstraintResult::Resolved;
3998
+ }
3995
3999
3996
4000
unsigned nestingDepth1 = T1->getNestingDepth ();
3997
4001
unsigned nestingDepth2 = T2->getNestingDepth ();
@@ -4005,9 +4009,12 @@ GenericSignatureBuilder::addSameTypeRequirementBetweenArchetypes(
4005
4009
}
4006
4010
4007
4011
// Merge the equivalence classes.
4008
- auto equivClass = T1->getOrCreateEquivalenceClass ();
4012
+ auto equivClass = T1->getOrCreateEquivalenceClass (* this );
4009
4013
equivClass->modified (*this );
4010
4014
4015
+ // Record the same-type constraint.
4016
+ equivClass->recordSameTypeConstraint (*this , OrigT1, OrigT2, Source);
4017
+
4011
4018
auto equivClass1Members = equivClass->members ;
4012
4019
auto equivClass2Members = T2->getEquivalenceClassMembers ();
4013
4020
for (auto equiv : equivClass2Members)
@@ -4137,7 +4144,7 @@ ConstraintResult GenericSignatureBuilder::addSameTypeRequirementToConcrete(
4137
4144
Type Concrete,
4138
4145
const RequirementSource *Source) {
4139
4146
auto rep = T->getRepresentative ();
4140
- auto equivClass = rep->getOrCreateEquivalenceClass ();
4147
+ auto equivClass = rep->getOrCreateEquivalenceClass (* this );
4141
4148
4142
4149
// Record the concrete type and its source.
4143
4150
equivClass->concreteTypeConstraints .push_back (
@@ -4889,7 +4896,7 @@ GenericSignatureBuilder::finalize(SourceLoc loc,
4889
4896
4890
4897
// Don't allow a generic parameter to be equivalent to a concrete type,
4891
4898
// because then we don't actually have a parameter.
4892
- auto equivClass = rep->getOrCreateEquivalenceClass ();
4899
+ auto equivClass = rep->getOrCreateEquivalenceClass (* this );
4893
4900
if (equivClass->concreteType ) {
4894
4901
if (auto constraint = equivClass->findAnyConcreteConstraintAsWritten ()){
4895
4902
Impl->HadAnyError = true ;
@@ -5436,7 +5443,7 @@ static void computeDerivedSameTypeComponents(
5436
5443
PotentialArchetype *rep,
5437
5444
llvm::SmallDenseMap<PotentialArchetype *, unsigned > &componentOf){
5438
5445
// Perform a depth-first search to identify the components.
5439
- auto equivClass = rep->getOrCreateEquivalenceClass ();
5446
+ auto equivClass = rep->getOrCreateEquivalenceClass (builder );
5440
5447
auto &components = equivClass->derivedSameTypeComponents ;
5441
5448
for (auto pa : rep->getEquivalenceClassMembers ()) {
5442
5449
// If we've already seen this potential archetype, there's nothing else to
@@ -6044,7 +6051,7 @@ void GenericSignatureBuilder::checkSameTypeConstraints(
6044
6051
void GenericSignatureBuilder::checkConcreteTypeConstraints (
6045
6052
ArrayRef<GenericTypeParamType *> genericParams,
6046
6053
PotentialArchetype *representative) {
6047
- auto equivClass = representative->getOrCreateEquivalenceClass ();
6054
+ auto equivClass = representative->getOrCreateEquivalenceClass (* this );
6048
6055
assert (equivClass->concreteType && " No concrete type to check" );
6049
6056
6050
6057
checkConstraintList<Type>(
@@ -6081,7 +6088,7 @@ void GenericSignatureBuilder::checkConcreteTypeConstraints(
6081
6088
void GenericSignatureBuilder::checkSuperclassConstraints (
6082
6089
ArrayRef<GenericTypeParamType *> genericParams,
6083
6090
PotentialArchetype *representative) {
6084
- auto equivClass = representative->getOrCreateEquivalenceClass ();
6091
+ auto equivClass = representative->getOrCreateEquivalenceClass (* this );
6085
6092
assert (equivClass->superclass && " No superclass constraint?" );
6086
6093
6087
6094
// FIXME: We should be substituting in the canonical type in context so
@@ -6236,7 +6243,7 @@ void GenericSignatureBuilder::enumerateRequirements(llvm::function_ref<
6236
6243
//
6237
6244
// FIXME: O(n) in the number of implied connected components within the
6238
6245
// equivalence class. The equivalence class should be small, but...
6239
- auto equivClass = archetype->getOrCreateEquivalenceClass ();
6246
+ auto equivClass = archetype->getOrCreateEquivalenceClass (* this );
6240
6247
6241
6248
assert (!equivClass->derivedSameTypeComponents .empty () &&
6242
6249
" Didn't compute derived same-type components?" );
0 commit comments