-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathPackConformance.cpp
236 lines (197 loc) · 8.78 KB
/
PackConformance.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
//===--- PackConformance.cpp - Variadic Protocol Conformance --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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 PackConformance structure, which describes the
// conformance of a type pack parameter to a protocol.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/PackConformance.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/InFlightSubstitution.h"
#include "swift/AST/Module.h"
#include "swift/AST/Types.h"
#include "swift/Basic/Assertions.h"
#define DEBUG_TYPE "AST"
using namespace swift;
void PackConformance::Profile(llvm::FoldingSetNodeID &ID) const {
Profile(ID, ConformingType, Protocol, getPatternConformances());
}
void PackConformance::Profile(llvm::FoldingSetNodeID &ID,
PackType *conformingType,
ProtocolDecl *protocol,
ArrayRef<ProtocolConformanceRef> conformances) {
ID.AddPointer(conformingType);
ID.AddPointer(protocol);
for (auto conformance : conformances)
ID.AddPointer(conformance.getOpaqueValue());
}
PackConformance::PackConformance(PackType *conformingType,
ProtocolDecl *protocol,
ArrayRef<ProtocolConformanceRef> conformances)
: ConformingType(conformingType), Protocol(protocol) {
assert(ConformingType->getNumElements() == conformances.size());
std::uninitialized_copy(conformances.begin(), conformances.end(),
getTrailingObjects<ProtocolConformanceRef>());
}
size_t PackConformance::numTrailingObjects(
OverloadToken<ProtocolConformanceRef>) const {
return ConformingType->getNumElements();
}
bool PackConformance::isInvalid() const {
return llvm::any_of(getPatternConformances(),
[&](const auto ref) { return ref.isInvalid(); });
}
ArrayRef<ProtocolConformanceRef>
PackConformance::getPatternConformances() const {
return {getTrailingObjects<ProtocolConformanceRef>(),
ConformingType->getNumElements()};
}
bool PackConformance::isCanonical() const {
if (!ConformingType->isCanonical())
return false;
for (auto conformance : getPatternConformances())
if (!conformance.isCanonical())
return false;
return true;
}
PackConformance *PackConformance::getCanonicalConformance() const {
if (isCanonical())
return const_cast<PackConformance *>(this);
SmallVector<ProtocolConformanceRef, 4> conformances;
for (auto conformance : getPatternConformances())
conformances.push_back(conformance.getCanonicalConformanceRef());
auto canonical = PackConformance::get(
cast<PackType>(ConformingType->getCanonicalType()),
Protocol, conformances);
assert(canonical->isCanonical());
return canonical;
}
/// Project the corresponding associated type from each pack element
/// of the conforming type, collecting the results into a new pack type
/// that has the same pack expansion structure as the conforming type.
PackType *PackConformance::getTypeWitness(
AssociatedTypeDecl *assocType,
SubstOptions options) const {
SmallVector<Type, 4> packElements;
auto conformances = getPatternConformances();
for (unsigned i : indices(conformances)) {
auto packElement = ConformingType->getElementType(i);
// If the pack element is a pack expansion, build a new pack expansion
// with the same count type as the original element, and the pattern
// type replaced with the associated type witness from the pattern
// conformance.
if (auto *packExpansion = packElement->getAs<PackExpansionType>()) {
auto assocTypePattern =
conformances[i].getTypeWitness(packExpansion->getPatternType(),
assocType, options);
packElements.push_back(PackExpansionType::get(
assocTypePattern, packExpansion->getCountType()));
// If the pack element is a scalar type, replace the scalar type with
// the associated type witness from the pattern conformance.
} else {
auto assocTypeScalar =
conformances[i].getTypeWitness(packElement, assocType, options);
packElements.push_back(assocTypeScalar);
}
}
return PackType::get(Protocol->getASTContext(), packElements);
}
/// Project the corresponding associated conformance from each pack element
/// of the conforming type, collecting the results into a new pack conformnace
/// whose conforming type has the same pack expansion structure as our
/// conforming type.
PackConformance *PackConformance::getAssociatedConformance(
Type assocType, ProtocolDecl *protocol) const {
SmallVector<Type, 4> packElements;
SmallVector<ProtocolConformanceRef, 4> packConformances;
auto conformances = getPatternConformances();
for (unsigned i : indices(conformances)) {
auto packElement = ConformingType->getElementType(i);
if (auto *packExpansion = packElement->getAs<PackExpansionType>()) {
auto assocTypePattern =
conformances[i].getAssociatedType(packExpansion->getPatternType(),
assocType);
packElements.push_back(PackExpansionType::get(
assocTypePattern, packExpansion->getCountType()));
auto assocConformancePattern =
conformances[i].getAssociatedConformance(packExpansion->getPatternType(),
assocType, protocol);
packConformances.push_back(assocConformancePattern);
} else {
auto assocTypeScalar =
conformances[i].getAssociatedType(packElement, assocType);
packElements.push_back(assocTypeScalar);
auto assocConformanceScalar =
conformances[i].getAssociatedConformance(packElement, assocType, protocol);
packConformances.push_back(assocConformanceScalar);
}
}
auto conformingType = PackType::get(Protocol->getASTContext(), packElements);
return PackConformance::get(conformingType, protocol, packConformances);
}
ProtocolConformanceRef PackConformance::subst(SubstitutionMap subMap,
SubstOptions options) const {
InFlightSubstitutionViaSubMap IFS(subMap, options);
return subst(IFS);
}
ProtocolConformanceRef PackConformance::subst(TypeSubstitutionFn subs,
LookupConformanceFn conformances,
SubstOptions options) const {
InFlightSubstitution IFS(subs, conformances, options);
return subst(IFS);
}
ProtocolConformanceRef
PackConformance::subst(InFlightSubstitution &IFS) const {
// Results built up by the expansion.
SmallVector<Type, 4> substElementTypes;
SmallVector<ProtocolConformanceRef, 4> substConformances;
auto origConformances = getPatternConformances();
assert(ConformingType->getNumElements() == origConformances.size());
for (auto i : range(ConformingType->getNumElements())) {
auto origElementType = ConformingType->getElementType(i);
if (auto *origExpansion = origElementType->getAs<PackExpansionType>()) {
// Substitute and expand an expansion element of the original pack.
IFS.expandPackExpansionType(origExpansion,
[&](Type substComponentType) {
substElementTypes.push_back(substComponentType);
// Just substitute the conformance. We don't directly represent
// pack expansion conformances here; it's sort of implicit in the
// corresponding pack element type.
substConformances.push_back(
origConformances[i].subst(origExpansion->getPatternType(), IFS));
});
} else {
// Substitute a scalar element of the original pack.
substElementTypes.push_back(origElementType.subst(IFS));
substConformances.push_back(
origConformances[i].subst(origElementType, IFS));
}
}
auto &ctx = Protocol->getASTContext();
auto *substConformingType = PackType::get(ctx, substElementTypes);
auto substConformance = PackConformance::get(substConformingType, Protocol,
substConformances);
return ProtocolConformanceRef(substConformance);
}
void swift::simple_display(llvm::raw_ostream &out, PackConformance *conformance) {
out << conformance->getType() << " : {";
bool first = true;
for (auto patternConformance : conformance->getPatternConformances()) {
if (first) {
out << ", ";
first = false;
}
simple_display(out, patternConformance);
}
out << "}";
}