forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericEnvironment.cpp
414 lines (340 loc) · 14.7 KB
/
GenericEnvironment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//===--- GenericEnvironment.cpp - GenericEnvironment AST ------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the GenericEnvironment class.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/GenericSignatureBuilder.h"
#include "swift/AST/ProtocolConformance.h"
using namespace swift;
GenericEnvironment::GenericEnvironment(GenericSignature *signature,
GenericSignatureBuilder *builder)
: Signature(signature), Builder(builder)
{
NumMappingsRecorded = 0;
NumArchetypeToInterfaceMappings = 0;
// Clear out the memory that holds the context types.
std::uninitialized_fill(getContextTypes().begin(), getContextTypes().end(),
Type());
}
/// Compute the depth of the \c DeclContext chain.
static unsigned declContextDepth(const DeclContext *dc) {
unsigned depth = 0;
while (auto parentDC = dc->getParent()) {
++depth;
dc = parentDC;
}
return depth;
}
void GenericEnvironment::setOwningDeclContext(DeclContext *newOwningDC) {
if (!OwningDC) {
OwningDC = newOwningDC;
return;
}
if (!newOwningDC || OwningDC == newOwningDC)
return;
// Find the least common ancestor context to be the owner.
unsigned oldDepth = declContextDepth(OwningDC);
unsigned newDepth = declContextDepth(newOwningDC);
while (oldDepth > newDepth) {
OwningDC = OwningDC->getParent();
--oldDepth;
}
while (newDepth > oldDepth) {
newOwningDC = newOwningDC->getParent();
--newDepth;
}
while (OwningDC != newOwningDC) {
OwningDC = OwningDC->getParent();
newOwningDC = newOwningDC->getParent();
}
}
void GenericEnvironment::addMapping(GenericParamKey key,
Type contextType) {
// Find the index into the parallel arrays of generic parameters and
// context types.
auto genericParams = Signature->getGenericParams();
unsigned index = key.findIndexIn(genericParams);
assert(genericParams[index] == key && "Bad generic parameter");
// Add the mapping from the generic parameter to the context type.
assert(getContextTypes()[index].isNull() && "Already recoded this mapping");
getContextTypes()[index] = contextType;
// If we mapped the generic parameter to an archetype, add it to the
// reverse mapping.
if (auto *archetype = contextType->getAs<ArchetypeType>()) {
auto genericParam = genericParams[index];
// Check whether we've already recorded a generic parameter for this
// archetype. Note that we always perform a linear search, because we
// won't have sorted the list yet.
bool found = false;
for (auto &mapping : getActiveArchetypeToInterfaceMappings()) {
if (mapping.first != archetype) continue;
// Multiple generic parameters map to the same archetype. If the
// existing entry comes from a later generic parameter, replace it with
// the earlier generic parameter. This gives us a deterministic reverse
// mapping.
auto otherGP = mapping.second->castTo<GenericTypeParamType>();
if (GenericParamKey(genericParam) < GenericParamKey(otherGP))
mapping.second = genericParam;
found = true;
break;
}
// If we haven't recorded a generic parameter for this archetype, do so now.
if (!found) {
void *ptr = getArchetypeToInterfaceMappingsBuffer().data()
+ NumArchetypeToInterfaceMappings;
new (ptr) ArchetypeToInterfaceMapping(archetype, genericParam);
++NumArchetypeToInterfaceMappings;
}
}
// Note that we've recorded this mapping.
++NumMappingsRecorded;
// If we've recorded all of the mappings, go ahead and sort the array of
// archetype-to-interface-type mappings.
if (NumMappingsRecorded == genericParams.size()) {
llvm::array_pod_sort(getActiveArchetypeToInterfaceMappings().begin(),
getActiveArchetypeToInterfaceMappings().end(),
[](const ArchetypeToInterfaceMapping *lhs,
const ArchetypeToInterfaceMapping *rhs) -> int {
std::less<ArchetypeType *> compare;
if (compare(lhs->first, rhs->first)) return -1;
if (compare(rhs->first, lhs->first)) return 1;
return 0;
});
}
}
Optional<Type> GenericEnvironment::getMappingIfPresent(
GenericParamKey key) const {
// Find the index into the parallel arrays of generic parameters and
// context types.
auto genericParams = Signature->getGenericParams();
unsigned index = key.findIndexIn(genericParams);
assert(genericParams[index] == key && "Bad generic parameter");
if (auto type = getContextTypes()[index])
return type;
return None;
}
bool GenericEnvironment::containsPrimaryArchetype(
ArchetypeType *archetype) const {
return static_cast<bool>(
QueryArchetypeToInterfaceSubstitutions(this)(archetype));
}
Type GenericEnvironment::mapTypeIntoContext(GenericEnvironment *env,
Type type) {
assert(!type->hasArchetype() && "already have a contextual type");
if (!env)
return type.substDependentTypesWithErrorTypes();
return env->mapTypeIntoContext(type);
}
Type
GenericEnvironment::mapTypeOutOfContext(GenericEnvironment *env,
Type type) {
assert(!type->hasTypeParameter() && "already have an interface type");
if (!env)
return type.substDependentTypesWithErrorTypes();
return env->mapTypeOutOfContext(type);
}
Type GenericEnvironment::mapTypeOutOfContext(Type type) const {
type = type.subst(QueryArchetypeToInterfaceSubstitutions(this),
MakeAbstractConformanceForGenericType(),
SubstFlags::AllowLoweredTypes);
assert(!type->hasArchetype() && "not fully substituted");
return type;
}
Type GenericEnvironment::QueryInterfaceTypeSubstitutions::operator()(
SubstitutableType *type) const {
if (auto gp = type->getAs<GenericTypeParamType>()) {
// Find the index into the parallel arrays of generic parameters and
// context types.
auto genericParams = self->Signature->getGenericParams();
GenericParamKey key(gp);
// Make sure that this generic parameter is from this environment.
unsigned index = key.findIndexIn(genericParams);
if (index == genericParams.size() || genericParams[index] != key)
return Type();
// If the context type isn't already known, lazily create it.
Type contextType = self->getContextTypes()[index];
if (!contextType) {
assert(self->Builder && "Missing generic signature builder for lazy query");
auto potentialArchetype = self->Builder->resolveArchetype(type);
auto mutableSelf = const_cast<GenericEnvironment *>(self);
contextType =
potentialArchetype->getTypeInContext(*mutableSelf->Builder,
mutableSelf);
// FIXME: Redundant mapping from key -> index.
if (self->getContextTypes()[index].isNull())
mutableSelf->addMapping(key, contextType);
}
return contextType;
}
return Type();
}
Type GenericEnvironment::QueryArchetypeToInterfaceSubstitutions::operator()(
SubstitutableType *type) const {
auto archetype = type->getAs<ArchetypeType>();
if (!archetype) return Type();
// If not all generic parameters have had their context types recorded,
// perform a linear search.
auto genericParams = self->Signature->getGenericParams();
unsigned numGenericParams = genericParams.size();
if (self->NumMappingsRecorded < numGenericParams) {
// Search through all of the active archetype-to-interface mappings.
for (auto &mapping : self->getActiveArchetypeToInterfaceMappings())
if (mapping.first == archetype) return mapping.second;
// We don't know if the archetype is from a different context or if we
// simply haven't recorded it yet. Spin through all of the generic
// parameters looking for one that provides this mapping.
for (auto gp : genericParams) {
// Map the generic parameter into our context. If we get back an
// archetype that matches, we're done.
auto gpArchetype = self->mapTypeIntoContext(gp)->getAs<ArchetypeType>();
if (gpArchetype == archetype) return gp;
}
// We have checked all of the generic parameters and not found anything;
// there is no substitution.
return Type();
}
// All generic parameters have ad their context types recorded, which means
// that the archetypes-to-interface-types array is sorted by address. Use a
// binary search.
struct MappingComparison {
bool operator()(const ArchetypeToInterfaceMapping &lhs,
const ArchetypeType *rhs) const {
std::less<const ArchetypeType *> compare;
return compare(lhs.first, rhs);
}
bool operator()(const ArchetypeType *lhs,
const ArchetypeToInterfaceMapping &rhs) const {
std::less<const ArchetypeType *> compare;
return compare(lhs, rhs.first);
}
bool operator()(const ArchetypeToInterfaceMapping &lhs,
const ArchetypeToInterfaceMapping &rhs) const {
std::less<const ArchetypeType *> compare;
return compare(lhs.first, rhs.first);
}
} mappingComparison;
auto mappings = self->getActiveArchetypeToInterfaceMappings();
auto known = std::lower_bound(mappings.begin(), mappings.end(), archetype,
mappingComparison);
if (known != mappings.end() && known->first == archetype)
return known->second;
return Type();
}
Type GenericEnvironment::mapTypeIntoContext(
Type type,
LookupConformanceFn lookupConformance) const {
assert(!type->hasOpenedExistential() &&
"Opened existentials are special and so are you");
Type result = type.subst(QueryInterfaceTypeSubstitutions(this),
lookupConformance,
(SubstFlags::AllowLoweredTypes|
SubstFlags::UseErrorType));
assert((!result->hasTypeParameter() || result->hasError()) &&
"not fully substituted");
return result;
}
Type GenericEnvironment::mapTypeIntoContext(Type type) const {
auto *sig = getGenericSignature();
return mapTypeIntoContext(type, LookUpConformanceInSignature(*sig));
}
Type GenericEnvironment::mapTypeIntoContext(GenericTypeParamType *type) const {
auto self = const_cast<GenericEnvironment *>(this);
Type result = QueryInterfaceTypeSubstitutions(self)(type);
if (!result)
return ErrorType::get(type);
return result;
}
GenericTypeParamType *GenericEnvironment::getSugaredType(
GenericTypeParamType *type) const {
for (auto *sugaredType : getGenericParams())
if (sugaredType->isEqual(type))
return sugaredType;
llvm_unreachable("missing generic parameter");
}
Type GenericEnvironment::getSugaredType(Type type) const {
if (!type->hasTypeParameter())
return type;
return type.transform([this](Type Ty) -> Type {
if (auto GP = dyn_cast<GenericTypeParamType>(Ty.getPointer())) {
return Type(getSugaredType(GP));
}
return Ty;
});
}
SubstitutionList
GenericEnvironment::getForwardingSubstitutions() const {
SmallVector<Substitution, 4> result;
getGenericSignature()->getSubstitutions(QueryInterfaceTypeSubstitutions(this),
MakeAbstractConformanceForGenericType(),
result);
return getGenericSignature()->getASTContext().AllocateCopy(result);
}
SubstitutionMap GenericEnvironment::
getSubstitutionMap(SubstitutionList subs) const {
SubstitutionMap result(const_cast<GenericEnvironment *>(this));
getGenericSignature()->enumeratePairedRequirements(
[&](Type depTy, ArrayRef<Requirement> reqts) -> bool {
// Map the interface type to a context type.
auto contextTy = depTy.subst(QueryInterfaceTypeSubstitutions(this),
MakeAbstractConformanceForGenericType());
auto sub = subs.front();
subs = subs.slice(1);
// Record the replacement type and its conformances.
if (auto *archetype = contextTy->getAs<ArchetypeType>()) {
result.addSubstitution(CanArchetypeType(archetype), sub.getReplacement());
assert(reqts.size() == sub.getConformances().size());
for (auto conformance : sub.getConformances())
result.addConformance(CanType(archetype), conformance);
return false;
}
assert(contextTy->hasError());
return false;
});
assert(subs.empty() && "did not use all substitutions?!");
result.verify();
return result;
}
SubstitutionMap
GenericEnvironment::
getSubstitutionMap(TypeSubstitutionFn subs,
GenericSignature::LookupConformanceFn lookupConformance) const {
SubstitutionMap subMap(const_cast<GenericEnvironment *>(this));
getGenericSignature()->enumeratePairedRequirements(
[&](Type depTy, ArrayRef<Requirement> reqs) -> bool {
// Map the interface type to a context type.
auto contextTy = depTy.subst(QueryInterfaceTypeSubstitutions(this),
MakeAbstractConformanceForGenericType());
// Compute the replacement type.
Type currentReplacement = contextTy.subst(subs, lookupConformance,
SubstFlags::UseErrorType);
if (auto archetypeTy = contextTy->getAs<ArchetypeType>()) {
subMap.addSubstitution(CanArchetypeType(archetypeTy),
currentReplacement);
// Collect the conformances.
for (auto req: reqs) {
assert(req.getKind() == RequirementKind::Conformance);
auto protoType = req.getSecondType()->castTo<ProtocolType>();
auto conformance = lookupConformance(CanArchetypeType(archetypeTy),
currentReplacement,
protoType);
if (conformance)
subMap.addConformance(CanArchetypeType(archetypeTy), *conformance);
}
}
return false;
});
subMap.verify();
return subMap;
}