Skip to content

Commit 1687895

Browse files
committed
AST: Refactor RequirementEnvironment and Witness to store GenericSignature instead of GenericEnvironment
1 parent 4f9b53f commit 1687895

File tree

6 files changed

+44
-50
lines changed

6 files changed

+44
-50
lines changed

include/swift/AST/RequirementEnvironment.h

+9-10
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,14 @@ class RequirementEnvironment {
7272
/// The witness thunk emitted by SILGen uses the synthetic signature.
7373
/// Therefore one invariant we preserve is that the witness thunk is
7474
/// ABI compatible with the requirement's function type.
75-
GenericSignature syntheticSignature = GenericSignature();
76-
GenericEnvironment *syntheticEnvironment = nullptr;
75+
GenericSignature syntheticSig = GenericSignature();
7776

7877
/// The generic signature of the protocol requirement member.
7978
GenericSignature reqSig = GenericSignature();
8079

8180
/// A substitution map mapping the requirement signature to the
8281
/// generic parameters of the synthetic signature.
83-
SubstitutionMap reqToSyntheticEnvMap;
82+
SubstitutionMap reqToSyntheticSigMap;
8483

8584
public:
8685
/// Create a new environment for matching the given requirement within a
@@ -102,20 +101,20 @@ class RequirementEnvironment {
102101
ClassDecl *covariantSelf,
103102
ProtocolConformance *conformance);
104103

105-
/// Retrieve the synthetic generic environment.
106-
GenericEnvironment *getSyntheticEnvironment() const {
107-
return syntheticEnvironment;
108-
}
109-
110104
/// Retrieve the generic signature of the requirement.
111105
GenericSignature getRequirementSignature() const {
112106
return reqSig;
113107
}
114108

109+
/// Retrieve the generic signature of the witness thunk.
110+
GenericSignature getSyntheticSignature() const {
111+
return syntheticSig;
112+
}
113+
115114
/// Retrieve the substitution map that maps the interface types of the
116-
/// requirement to the interface types of the synthetic environment.
115+
/// requirement to the interface types of the synthetic signature.
117116
SubstitutionMap getRequirementToSyntheticMap() const {
118-
return reqToSyntheticEnvMap;
117+
return reqToSyntheticSigMap;
119118
}
120119
};
121120

include/swift/AST/Witness.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ class Witness {
9292
/// The witness declaration, along with the substitutions needed to use
9393
/// the witness declaration from the synthetic environment.
9494
ConcreteDeclRef declRef;
95-
GenericEnvironment *syntheticEnvironment;
96-
SubstitutionMap reqToSyntheticEnvSubs;
95+
GenericSignature syntheticSig;
96+
SubstitutionMap reqToSyntheticSigSubs;
9797
/// The derivative generic signature, when the requirement is a derivative
9898
/// function.
9999
GenericSignature derivativeGenSig;
@@ -141,10 +141,10 @@ class Witness {
141141
/// \param substitutions The substitutions required to use the witness from
142142
/// the synthetic environment.
143143
///
144-
/// \param syntheticEnv The synthetic environment.
144+
/// \param syntheticSig The synthetic signature.
145145
///
146-
/// \param reqToSyntheticEnvSubs The mapping from the interface types of the
147-
/// requirement into the interface types of the synthetic environment.
146+
/// \param reqToSyntheticSigSubs The mapping from the interface types of the
147+
/// requirement into the interface types of the synthetic signature.
148148
///
149149
/// \param derivativeGenSig The derivative generic signature, when the
150150
/// requirement is a derivative function.
@@ -153,8 +153,8 @@ class Witness {
153153
/// need to hop to before calling the witness.
154154
Witness(ValueDecl *decl,
155155
SubstitutionMap substitutions,
156-
GenericEnvironment *syntheticEnv,
157-
SubstitutionMap reqToSyntheticEnvSubs,
156+
GenericSignature syntheticSig,
157+
SubstitutionMap reqToSyntheticSigSubs,
158158
GenericSignature derivativeGenSig,
159159
Optional<ActorIsolation> enterIsolation);
160160

@@ -183,18 +183,18 @@ class Witness {
183183
return getDeclRef().getSubstitutions();
184184
}
185185

186-
/// Retrieve the synthetic generic environment.
187-
GenericEnvironment *getSyntheticEnvironment() const {
186+
/// Retrieve the synthetic generic signature.
187+
GenericSignature getSyntheticSignature() const {
188188
if (auto *storedWitness = storage.dyn_cast<StoredWitness *>())
189-
return storedWitness->syntheticEnvironment;
189+
return storedWitness->syntheticSig;
190190
return nullptr;
191191
}
192192

193193
/// Retrieve the substitution map that maps the interface types of the
194-
/// requirement to the interface types of the synthetic environment.
194+
/// requirement to the interface types of the synthetic signature.
195195
SubstitutionMap getRequirementToSyntheticSubs() const {
196196
if (auto *storedWitness = storage.dyn_cast<StoredWitness *>())
197-
return storedWitness->reqToSyntheticEnvSubs;
197+
return storedWitness->reqToSyntheticSigSubs;
198198
return {};
199199
}
200200

lib/AST/ProtocolConformance.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,28 @@ STATISTIC(NumConformanceLookupTables, "# of conformance lookup tables built");
4242
using namespace swift;
4343

4444
Witness::Witness(ValueDecl *decl, SubstitutionMap substitutions,
45-
GenericEnvironment *syntheticEnv,
46-
SubstitutionMap reqToSynthesizedEnvSubs,
45+
GenericSignature syntheticSig,
46+
SubstitutionMap reqToSyntheticSigSubs,
4747
GenericSignature derivativeGenSig,
4848
Optional<ActorIsolation> enterIsolation) {
49-
if (!syntheticEnv && substitutions.empty() &&
50-
reqToSynthesizedEnvSubs.empty() && !enterIsolation) {
49+
if (!syntheticSig && substitutions.empty() &&
50+
reqToSyntheticSigSubs.empty() && !enterIsolation) {
5151
storage = decl;
5252
return;
5353
}
5454

5555
auto &ctx = decl->getASTContext();
5656
auto declRef = ConcreteDeclRef(decl, substitutions);
5757
auto storedMem = ctx.Allocate(sizeof(StoredWitness), alignof(StoredWitness));
58-
auto stored = new (storedMem) StoredWitness{declRef, syntheticEnv,
59-
reqToSynthesizedEnvSubs,
58+
auto stored = new (storedMem) StoredWitness{declRef, syntheticSig,
59+
reqToSyntheticSigSubs,
6060
derivativeGenSig, enterIsolation};
6161

6262
storage = stored;
6363
}
6464

6565
Witness Witness::withEnterIsolation(ActorIsolation enterIsolation) const {
66-
return Witness(getDecl(), getSubstitutions(), getSyntheticEnvironment(),
66+
return Witness(getDecl(), getSubstitutions(), getSyntheticSignature(),
6767
getRequirementToSyntheticSubs(),
6868
getDerivativeGenericSignature(), enterIsolation);
6969
}

lib/AST/RequirementEnvironment.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ RequirementEnvironment::RequirementEnvironment(
8888
auto selfType = cast<GenericTypeParamType>(
8989
proto->getSelfInterfaceType()->getCanonicalType());
9090

91-
reqToSyntheticEnvMap = SubstitutionMap::get(reqSig,
91+
reqToSyntheticSigMap = SubstitutionMap::get(reqSig,
9292
[selfType, substConcreteType, depth, covariantSelf, &ctx]
9393
(SubstitutableType *type) -> Type {
9494
// If the conforming type is a class, the protocol 'Self' maps to
@@ -141,10 +141,7 @@ RequirementEnvironment::RequirementEnvironment(
141141
if (!covariantSelf &&
142142
reqSig.getGenericParams().size() == 1 &&
143143
reqSig.getRequirements().size() == 1) {
144-
syntheticSignature = conformanceDC->getGenericSignatureOfContext().getCanonicalSignature();
145-
syntheticEnvironment =
146-
syntheticSignature.getGenericEnvironment();
147-
144+
syntheticSig = conformanceDC->getGenericSignatureOfContext().getCanonicalSignature();
148145
return;
149146
}
150147

@@ -207,13 +204,12 @@ RequirementEnvironment::RequirementEnvironment(
207204
// Next, add each of the requirements (mapped from the requirement's
208205
// interface types into the abstract type parameters).
209206
for (auto &rawReq : reqSig.getRequirements()) {
210-
if (auto req = rawReq.subst(reqToSyntheticEnvMap))
207+
if (auto req = rawReq.subst(reqToSyntheticSigMap))
211208
requirements.push_back(*req);
212209
}
213210

214211
// Produce the generic signature and environment.
215-
syntheticSignature = buildGenericSignature(ctx, GenericSignature(),
216-
std::move(genericParamTypes),
217-
std::move(requirements));
218-
syntheticEnvironment = syntheticSignature.getGenericEnvironment();
212+
syntheticSig = buildGenericSignature(ctx, GenericSignature(),
213+
std::move(genericParamTypes),
214+
std::move(requirements));
219215
}

lib/SILGen/SILGenType.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,8 @@ SILFunction *SILGenModule::emitProtocolWitness(
726726
auto reqtSubMap = witness.getRequirementToSyntheticSubs();
727727

728728
// The generic environment for the witness thunk.
729-
auto *genericEnv = witness.getSyntheticEnvironment();
730-
CanGenericSignature genericSig;
731-
if (genericEnv)
732-
genericSig = genericEnv->getGenericSignature().getCanonicalSignature();
729+
auto *genericEnv = witness.getSyntheticSignature().getGenericEnvironment();
730+
auto genericSig = witness.getSyntheticSignature().getCanonicalSignature();
733731

734732
// The type of the witness thunk.
735733
auto reqtSubstTy = cast<AnyFunctionType>(

lib/Sema/TypeCheckProtocol.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ struct swift::RequirementCheck {
9999
};
100100

101101
swift::Witness RequirementMatch::getWitness(ASTContext &ctx) const {
102-
auto syntheticEnv = ReqEnv->getSyntheticEnvironment();
103102
return swift::Witness(this->Witness, WitnessSubstitutions,
104-
syntheticEnv, ReqEnv->getRequirementToSyntheticMap(),
103+
ReqEnv->getSyntheticSignature(),
104+
ReqEnv->getRequirementToSyntheticMap(),
105105
DerivativeGenSig, None);
106106
}
107107

@@ -1077,12 +1077,13 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
10771077
// the required type and the witness type.
10781078
cs.emplace(dc, ConstraintSystemFlags::AllowFixes);
10791079

1080-
auto reqGenericEnv = reqEnvironment.getSyntheticEnvironment();
1080+
auto syntheticSig = reqEnvironment.getSyntheticSignature();
1081+
auto *syntheticEnv = syntheticSig.getGenericEnvironment();
10811082
auto reqSubMap = reqEnvironment.getRequirementToSyntheticMap();
10821083

10831084
Type selfTy = proto->getSelfInterfaceType().subst(reqSubMap);
1084-
if (reqGenericEnv)
1085-
selfTy = reqGenericEnv->mapTypeIntoContext(selfTy);
1085+
if (syntheticEnv)
1086+
selfTy = syntheticEnv->mapTypeIntoContext(selfTy);
10861087

10871088
// Open up the type of the requirement.
10881089
reqLocator = cs->getConstraintLocator(
@@ -1106,8 +1107,8 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
11061107
if (replacedInReq->hasError())
11071108
continue;
11081109

1109-
if (reqGenericEnv) {
1110-
replacedInReq = reqGenericEnv->mapTypeIntoContext(replacedInReq);
1110+
if (syntheticEnv) {
1111+
replacedInReq = syntheticEnv->mapTypeIntoContext(replacedInReq);
11111112
}
11121113

11131114
cs->addConstraint(ConstraintKind::Bind, replacement.second, replacedInReq,

0 commit comments

Comments
 (0)