@@ -25,9 +25,6 @@ GenericEnvironment::GenericEnvironment(GenericSignature *signature,
25
25
GenericSignatureBuilder *builder)
26
26
: Signature(signature), Builder(builder)
27
27
{
28
- NumMappingsRecorded = 0 ;
29
- NumArchetypeToInterfaceMappings = 0 ;
30
-
31
28
// Clear out the memory that holds the context types.
32
29
std::uninitialized_fill (getContextTypes ().begin (), getContextTypes ().end (),
33
30
Type ());
@@ -73,55 +70,6 @@ void GenericEnvironment::addMapping(GenericParamKey key,
73
70
// Add the mapping from the generic parameter to the context type.
74
71
assert (getContextTypes ()[index].isNull () && " Already recoded this mapping" );
75
72
getContextTypes ()[index] = contextType;
76
-
77
- // If we mapped the generic parameter to an archetype, add it to the
78
- // reverse mapping.
79
- if (auto *archetype = contextType->getAs <ArchetypeType>()) {
80
- auto genericParam = genericParams[index];
81
-
82
- // Check whether we've already recorded a generic parameter for this
83
- // archetype. Note that we always perform a linear search, because we
84
- // won't have sorted the list yet.
85
- bool found = false ;
86
- for (auto &mapping : getActiveArchetypeToInterfaceMappings ()) {
87
- if (mapping.first != archetype) continue ;
88
-
89
- // Multiple generic parameters map to the same archetype. If the
90
- // existing entry comes from a later generic parameter, replace it with
91
- // the earlier generic parameter. This gives us a deterministic reverse
92
- // mapping.
93
- auto otherGP = mapping.second ->castTo <GenericTypeParamType>();
94
- if (GenericParamKey (genericParam) < GenericParamKey (otherGP))
95
- mapping.second = genericParam;
96
- found = true ;
97
- break ;
98
- }
99
-
100
- // If we haven't recorded a generic parameter for this archetype, do so now.
101
- if (!found) {
102
- void *ptr = getArchetypeToInterfaceMappingsBuffer ().data ()
103
- + NumArchetypeToInterfaceMappings;
104
- new (ptr) ArchetypeToInterfaceMapping (archetype, genericParam);
105
- ++NumArchetypeToInterfaceMappings;
106
- }
107
- }
108
-
109
- // Note that we've recorded this mapping.
110
- ++NumMappingsRecorded;
111
-
112
- // If we've recorded all of the mappings, go ahead and sort the array of
113
- // archetype-to-interface-type mappings.
114
- if (NumMappingsRecorded == genericParams.size ()) {
115
- llvm::array_pod_sort (getActiveArchetypeToInterfaceMappings ().begin (),
116
- getActiveArchetypeToInterfaceMappings ().end (),
117
- [](const ArchetypeToInterfaceMapping *lhs,
118
- const ArchetypeToInterfaceMapping *rhs) -> int {
119
- std::less<ArchetypeType *> compare;
120
- if (compare (lhs->first , rhs->first )) return -1 ;
121
- if (compare (rhs->first , lhs->first )) return 1 ;
122
- return 0 ;
123
- });
124
- }
125
73
}
126
74
127
75
Optional<Type> GenericEnvironment::getMappingIfPresent (
@@ -160,7 +108,9 @@ GenericEnvironment::mapTypeOutOfContext(GenericEnvironment *env,
160
108
}
161
109
162
110
Type GenericEnvironment::mapTypeOutOfContext (Type type) const {
163
- type = type.subst (QueryArchetypeToInterfaceSubstitutions (this ),
111
+ type = type.subst ([&](SubstitutableType *t) -> Type {
112
+ return cast<ArchetypeType>(t)->getInterfaceType ();
113
+ },
164
114
MakeAbstractConformanceForGenericType (),
165
115
SubstFlags::AllowLoweredTypes);
166
116
assert (!type->hasArchetype () && " not fully substituted" );
@@ -204,74 +154,6 @@ Type GenericEnvironment::QueryInterfaceTypeSubstitutions::operator()(
204
154
return Type ();
205
155
}
206
156
207
- Type GenericEnvironment::QueryArchetypeToInterfaceSubstitutions::operator ()(
208
- SubstitutableType *type) const {
209
- auto archetype = type->getAs <ArchetypeType>();
210
- if (!archetype) return Type ();
211
-
212
- // Only top-level archetypes need to be substituted directly; nested
213
- // archetypes will be handled via their root archetypes.
214
- if (archetype->getParent ()) return Type ();
215
-
216
- // If not all generic parameters have had their context types recorded,
217
- // perform a linear search.
218
- auto genericParams = self->Signature ->getGenericParams ();
219
- unsigned numGenericParams = genericParams.size ();
220
- if (self->NumMappingsRecorded < numGenericParams) {
221
- // Search through all of the active archetype-to-interface mappings.
222
- for (auto &mapping : self->getActiveArchetypeToInterfaceMappings ())
223
- if (mapping.first == archetype) return mapping.second ;
224
-
225
- // We don't know if the archetype is from a different context or if we
226
- // simply haven't recorded it yet. Spin through all of the generic
227
- // parameters looking for one that provides this mapping.
228
- for (auto gp : genericParams) {
229
- // Map the generic parameter into our context. If we get back an
230
- // archetype that matches, we're done.
231
- auto gpArchetype = self->mapTypeIntoContext (gp)->getAs <ArchetypeType>();
232
- if (gpArchetype == archetype) return gp;
233
- }
234
-
235
- // We have checked all of the generic parameters and not found anything;
236
- // there is no substitution.
237
- return Type ();
238
- }
239
-
240
- // All generic parameters have ad their context types recorded, which means
241
- // that the archetypes-to-interface-types array is sorted by address. Use a
242
- // binary search.
243
- struct MappingComparison {
244
- bool operator ()(const ArchetypeToInterfaceMapping &lhs,
245
- const ArchetypeType *rhs) const {
246
- std::less<const ArchetypeType *> compare;
247
-
248
- return compare (lhs.first , rhs);
249
- }
250
-
251
- bool operator ()(const ArchetypeType *lhs,
252
- const ArchetypeToInterfaceMapping &rhs) const {
253
- std::less<const ArchetypeType *> compare;
254
-
255
- return compare (lhs, rhs.first );
256
- }
257
-
258
- bool operator ()(const ArchetypeToInterfaceMapping &lhs,
259
- const ArchetypeToInterfaceMapping &rhs) const {
260
- std::less<const ArchetypeType *> compare;
261
-
262
- return compare (lhs.first , rhs.first );
263
- }
264
- } mappingComparison;
265
-
266
- auto mappings = self->getActiveArchetypeToInterfaceMappings ();
267
- auto known = std::lower_bound (mappings.begin (), mappings.end (), archetype,
268
- mappingComparison);
269
- if (known != mappings.end () && known->first == archetype)
270
- return known->second ;
271
-
272
- return Type ();
273
- }
274
-
275
157
Type GenericEnvironment::mapTypeIntoContext (
276
158
Type type,
277
159
LookupConformanceFn lookupConformance) const {
0 commit comments