@@ -2042,22 +2042,6 @@ TypeDecl *EquivalenceClass::lookupNestedType(
2042
2042
return populateResult ((nestedTypeNameCache[name] = std::move (entry)));
2043
2043
}
2044
2044
2045
- // / Determine whether any part of this potential archetype's path to the
2046
- // / root contains the given equivalence class.
2047
- static bool pathContainsEquivalenceClass (GenericSignatureBuilder &builder,
2048
- PotentialArchetype *pa,
2049
- EquivalenceClass *equivClass) {
2050
- // Chase the potential archetype up to the root.
2051
- for (; pa; pa = pa->getParent ()) {
2052
- // Check whether this potential archetype is in the given equivalence
2053
- // class.
2054
- if (pa->getOrCreateEquivalenceClass (builder) == equivClass)
2055
- return true ;
2056
- }
2057
-
2058
- return false ;
2059
- }
2060
-
2061
2045
Type EquivalenceClass::getAnchor (
2062
2046
GenericSignatureBuilder &builder,
2063
2047
TypeArrayView<GenericTypeParamType> genericParams) {
@@ -2097,54 +2081,26 @@ Type EquivalenceClass::getAnchor(
2097
2081
2098
2082
// If we saw a generic parameter, ignore any nested types.
2099
2083
if (bestGenericParam) continue ;
2100
-
2101
- // If the nested type doesn't have an associated type, skip it.
2102
- auto assocType = member->getResolvedAssociatedType ();
2103
- if (!assocType) continue ;
2104
-
2105
- // Dig out the equivalence class of the parent.
2106
- auto parentEquivClass =
2107
- member->getParent ()->getOrCreateEquivalenceClass (builder);
2108
-
2109
- // If the path from this member to the root contains this equivalence
2110
- // class, it cannot be part of the anchor.
2111
- if (pathContainsEquivalenceClass (builder, member->getParent (), this ))
2112
- continue ;
2113
-
2114
- // Take the best associated type for this equivalence class.
2115
- assocType = assocType->getAssociatedTypeAnchor ();
2116
- auto &bestAssocType = nestedTypes[parentEquivClass];
2117
- if (!bestAssocType ||
2118
- compareAssociatedTypes (assocType, bestAssocType) < 0 )
2119
- bestAssocType = assocType;
2120
2084
}
2121
2085
2122
2086
// If we found a generic parameter, return that.
2123
2087
if (bestGenericParam)
2124
2088
return bestGenericParam;
2125
2089
2126
- // Determine the best anchor among the parent equivalence classes.
2127
- Type bestParentAnchor;
2128
- AssociatedTypeDecl *bestAssocType = nullptr ;
2129
- std::pair<EquivalenceClass *, Identifier> bestNestedType;
2130
- for (const auto &nestedType : nestedTypes) {
2131
- auto parentAnchor = nestedType.first ->getAnchor (builder, genericParams);
2132
- if (!bestParentAnchor ||
2133
- compareDependentTypes (parentAnchor, bestParentAnchor) < 0 ) {
2134
- bestParentAnchor = parentAnchor;
2135
- bestAssocType = nestedType.second ;
2136
- }
2137
- }
2138
-
2139
- // Form the anchor type.
2140
- Type anchorType = DependentMemberType::get (bestParentAnchor, bestAssocType);
2090
+ // Form the anchor.
2091
+ for (auto member : members) {
2092
+ auto anchorType =
2093
+ builder.simplifyType (member->getDependentType (genericParams));
2094
+ if (!anchorType) continue ;
2141
2095
2142
- // Record the cache miss and update the cache.
2143
- ++NumArchetypeAnchorCacheMisses;
2144
- archetypeAnchorCache.anchor = anchorType;
2145
- archetypeAnchorCache.numMembers = members.size ();
2096
+ // Record the cache miss and update the cache.
2097
+ ++NumArchetypeAnchorCacheMisses;
2098
+ archetypeAnchorCache.anchor = anchorType;
2099
+ archetypeAnchorCache.numMembers = members.size ();
2100
+ return anchorType;
2101
+ }
2146
2102
2147
- return anchorType ;
2103
+ llvm_unreachable ( " Unable to compute anchor " ) ;
2148
2104
}
2149
2105
2150
2106
Type EquivalenceClass::getTypeInContext (GenericSignatureBuilder &builder,
@@ -3254,19 +3210,48 @@ unpackPath(PotentialArchetype *type,
3254
3210
return type->getGenericParamKey ();
3255
3211
}
3256
3212
3257
- // / Form a dependent type with the (canonical) generic parameter for the given
3258
- // / parameter key, then following the path of associated types.
3259
- static Type formDependentType (ASTContext &ctx, GenericParamKey genericParam,
3213
+ // / Unpack the given dependent type into a path from a generic parameter
3214
+ // / through a sequence of associated type declarations.
3215
+ // /
3216
+ // / \returns the generic parameter at the root of the type, or NULL if
3217
+ // / this is not a well-formed dependent member type. along the path doesn't
3218
+ // / have an associated type.
3219
+ static GenericTypeParamType *
3220
+ unpackPath (Type type, SmallVectorImpl<AssociatedTypeDecl *> &path) {
3221
+ if (auto depMemTy = type->getAs <DependentMemberType>()) {
3222
+ auto result = unpackPath (depMemTy->getBase (), path);
3223
+ if (!result) return nullptr ;
3224
+
3225
+ auto assocType = depMemTy->getAssocType ();
3226
+ if (!assocType) return nullptr ;
3227
+
3228
+ path.push_back (assocType);
3229
+ return result;
3230
+ }
3231
+
3232
+ return type->getAs <GenericTypeParamType>();
3233
+ }
3234
+
3235
+ // / Form a dependent type with the given generic parameter, then following the
3236
+ // / path of associated types.
3237
+ static Type formDependentType (GenericTypeParamType *base,
3260
3238
ArrayRef<AssociatedTypeDecl *> path) {
3261
- return std::accumulate (path.begin (), path.end (),
3262
- Type (GenericTypeParamType::get (genericParam.Depth ,
3263
- genericParam.Index ,
3264
- ctx)),
3239
+ return std::accumulate (path.begin (), path.end (), Type (base),
3265
3240
[](Type type, AssociatedTypeDecl *assocType) -> Type {
3266
3241
return DependentMemberType::get (type, assocType);
3267
3242
});
3268
3243
}
3269
3244
3245
+ // / Form a dependent type with the (canonical) generic parameter for the given
3246
+ // / parameter key, then following the path of associated types.
3247
+ static Type formDependentType (ASTContext &ctx, GenericParamKey genericParam,
3248
+ ArrayRef<AssociatedTypeDecl *> path) {
3249
+ return formDependentType (GenericTypeParamType::get (genericParam.Depth ,
3250
+ genericParam.Index ,
3251
+ ctx),
3252
+ path);
3253
+ }
3254
+
3270
3255
RewriteTreeNode *
3271
3256
GenericSignatureBuilder::Implementation::getRewriteTreeRootIfPresent (
3272
3257
const EquivalenceClass *equivClass) {
@@ -3390,6 +3375,16 @@ bool GenericSignatureBuilder::simplifyType(
3390
3375
return simplified;
3391
3376
}
3392
3377
3378
+ Type GenericSignatureBuilder::simplifyType (Type type) {
3379
+ SmallVector<AssociatedTypeDecl *, 4 > path;
3380
+ auto base = unpackPath (type, path);
3381
+ if (!base) return nullptr ;
3382
+
3383
+ if (!simplifyType (base, path)) return type;
3384
+
3385
+ return formDependentType (base, path);
3386
+ }
3387
+
3393
3388
#pragma mark Equivalence classes
3394
3389
EquivalenceClass::EquivalenceClass (PotentialArchetype *representative)
3395
3390
: recursiveConcreteType(false ), invalidConcreteType(false ),
0 commit comments