Skip to content

Commit fa30159

Browse files
committed
RequirementMachine: Move term to type methods from RewriteContext to PropertyMap
1 parent 86f17a7 commit fa30159

File tree

6 files changed

+82
-47
lines changed

6 files changed

+82
-47
lines changed

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ RequirementMachine::getLocalRequirements(
3838
verify(term);
3939

4040
GenericSignature::LocalRequirements result;
41-
result.anchor = Context.getTypeForTerm(term, genericParams);
41+
result.anchor = Map.getTypeForTerm(term, genericParams);
4242

4343
auto *props = Map.lookUpProperties(term);
4444
if (!props)
4545
return result;
4646

4747
if (props->isConcreteType()) {
48-
result.concreteType = props->getConcreteType({}, term, Context);
48+
result.concreteType = props->getConcreteType({}, term, Map);
4949
return result;
5050
}
5151

5252
if (props->hasSuperclassBound()) {
53-
result.superclass = props->getSuperclassBound({}, term, Context);
53+
result.superclass = props->getSuperclassBound({}, term, Map);
5454
}
5555

5656
for (const auto *proto : props->getConformsToExcludingSuperclassConformances())
@@ -152,7 +152,7 @@ getSuperclassBound(Type depType,
152152
if (!props->hasSuperclassBound())
153153
return Type();
154154

155-
return props->getSuperclassBound(genericParams, term, Context);
155+
return props->getSuperclassBound(genericParams, term, Map);
156156
}
157157

158158
bool RequirementMachine::isConcreteType(Type depType) const {
@@ -183,7 +183,7 @@ getConcreteType(Type depType,
183183
if (!props->isConcreteType())
184184
return Type();
185185

186-
return props->getConcreteType(genericParams, term, Context);
186+
return props->getConcreteType(genericParams, term, Map);
187187
}
188188

189189
bool RequirementMachine::areSameTypeParameterInContext(Type depType1,
@@ -277,7 +277,7 @@ bool RequirementMachine::isCanonicalTypeInContext(Type type) const {
277277
Self.System.simplify(term);
278278
Self.verify(term);
279279

280-
auto anchor = Self.Context.getTypeForTerm(term, {});
280+
auto anchor = Self.Map.getTypeForTerm(term, {});
281281
if (CanType(anchor) != CanType(component))
282282
return Action::Stop;
283283

@@ -353,7 +353,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
353353
if (props) {
354354
if (props->isConcreteType()) {
355355
auto concreteType = props->getConcreteType(genericParams,
356-
prefix, Context);
356+
prefix, Map);
357357
if (!concreteType->hasTypeParameter())
358358
return concreteType;
359359

@@ -368,7 +368,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
368368
if (props->hasSuperclassBound() &&
369369
prefix.size() != term.size()) {
370370
auto superclass = props->getSuperclassBound(genericParams,
371-
prefix, Context);
371+
prefix, Map);
372372
if (!superclass->hasTypeParameter())
373373
return superclass;
374374

@@ -377,7 +377,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
377377
}
378378
}
379379

380-
return Context.getTypeForTerm(prefix, genericParams);
380+
return Map.getTypeForTerm(prefix, genericParams);
381381
}();
382382

383383
// If T is already valid, the longest valid prefix U of T is T itself, and
@@ -402,7 +402,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
402402

403403
// Compute the type of the unresolved suffix term V, rooted in the
404404
// generic parameter τ_0_0.
405-
auto origType = Context.getRelativeTypeForTerm(term, prefix);
405+
auto origType = Map.getRelativeTypeForTerm(term, prefix);
406406

407407
// Substitute τ_0_0 in the above relative type with the concrete type
408408
// for U.

lib/AST/RequirementMachine/InterfaceType.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// Type to term conversion is implemented on the RewriteContext, and does not
2424
// depend on the specific RewriteSystem used.
2525
//
26-
// Term to type conversion is implemented on the RewriteSystem, and must only
26+
// Term to type conversion is implemented on the PropertyMap, and must only
2727
// be performed after completion. This is because it relies on the property map
2828
// to map associated type symbols back to Swift types.
2929
//
@@ -40,9 +40,13 @@
4040

4141
#include "swift/AST/Decl.h"
4242
#include "swift/AST/Types.h"
43+
#include "PropertyMap.h"
4344
#include "RewriteSystem.h"
4445
#include "RewriteContext.h"
4546

47+
using namespace swift;
48+
using namespace rewriting;
49+
4650
Term RewriteContext::getTermForType(CanType paramType,
4751
const ProtocolDecl *proto) {
4852
return Term::get(getMutableTermForType(paramType, proto), *this);
@@ -210,7 +214,8 @@ AssociatedTypeDecl *RewriteContext::getAssociatedTypeForSymbol(Symbol symbol) {
210214
template<typename Iter>
211215
Type getTypeForSymbolRange(Iter begin, Iter end, Type root,
212216
TypeArrayView<GenericTypeParamType> genericParams,
213-
const RewriteContext &ctx) {
217+
const PropertyMap &map) {
218+
auto &ctx = map.getRewriteContext();
214219
Type result = root;
215220

216221
auto handleRoot = [&](GenericTypeParamType *genericParam) {
@@ -286,32 +291,32 @@ Type getTypeForSymbolRange(Iter begin, Iter end, Type root,
286291

287292
// We should have a resolved type at this point.
288293
auto *assocType =
289-
const_cast<RewriteContext &>(ctx)
290-
.getAssociatedTypeForSymbol(symbol);
294+
ctx.getAssociatedTypeForSymbol(symbol);
291295
result = DependentMemberType::get(result, assocType);
292296
}
293297

294298
return result;
295299
}
296300

297-
Type RewriteContext::getTypeForTerm(Term term,
301+
Type PropertyMap::getTypeForTerm(Term term,
298302
TypeArrayView<GenericTypeParamType> genericParams) const {
299303
return getTypeForSymbolRange(term.begin(), term.end(), Type(),
300304
genericParams, *this);
301305
}
302306

303-
Type RewriteContext::getTypeForTerm(const MutableTerm &term,
307+
Type PropertyMap::getTypeForTerm(const MutableTerm &term,
304308
TypeArrayView<GenericTypeParamType> genericParams) const {
305309
return getTypeForSymbolRange(term.begin(), term.end(), Type(),
306310
genericParams, *this);
307311
}
308312

309-
Type RewriteContext::getRelativeTypeForTerm(
313+
Type PropertyMap::getRelativeTypeForTerm(
310314
const MutableTerm &term, const MutableTerm &prefix) const {
311315
assert(std::equal(prefix.begin(), prefix.end(), term.begin()));
312316

313317
auto genericParam =
314-
CanGenericTypeParamType::get(/*type sequence*/ false, 0, 0, Context);
318+
CanGenericTypeParamType::get(/*type sequence*/ false, 0, 0,
319+
Context.getASTContext());
315320
return getTypeForSymbolRange(
316321
term.begin() + prefix.size(), term.end(), genericParam,
317322
{ }, *this);
@@ -389,7 +394,7 @@ RewriteContext::getRelativeTermForType(CanType typeWitness,
389394

390395
/// Reverses the transformation performed by
391396
/// RewriteSystemBuilder::getConcreteSubstitutionSchema().
392-
Type RewriteContext::getTypeFromSubstitutionSchema(
397+
Type PropertyMap::getTypeFromSubstitutionSchema(
393398
Type schema, ArrayRef<Term> substitutions,
394399
TypeArrayView<GenericTypeParamType> genericParams,
395400
const MutableTerm &prefix) const {

lib/AST/RequirementMachine/PropertyMap.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ PropertyBag::getPrefixAfterStrippingKey(const MutableTerm &lookupTerm) const {
152152
Type PropertyBag::getSuperclassBound(
153153
TypeArrayView<GenericTypeParamType> genericParams,
154154
const MutableTerm &lookupTerm,
155-
RewriteContext &ctx) const {
155+
const PropertyMap &map) const {
156156
MutableTerm prefix = getPrefixAfterStrippingKey(lookupTerm);
157-
return ctx.getTypeFromSubstitutionSchema(Superclass->getSuperclass(),
157+
return map.getTypeFromSubstitutionSchema(Superclass->getSuperclass(),
158158
Superclass->getSubstitutions(),
159159
genericParams, prefix);
160160
}
@@ -171,9 +171,9 @@ Type PropertyBag::getSuperclassBound(
171171
Type PropertyBag::getConcreteType(
172172
TypeArrayView<GenericTypeParamType> genericParams,
173173
const MutableTerm &lookupTerm,
174-
RewriteContext &ctx) const {
174+
const PropertyMap &map) const {
175175
MutableTerm prefix = getPrefixAfterStrippingKey(lookupTerm);
176-
return ctx.getTypeFromSubstitutionSchema(ConcreteType->getConcreteType(),
176+
return map.getTypeFromSubstitutionSchema(ConcreteType->getConcreteType(),
177177
ConcreteType->getSubstitutions(),
178178
genericParams, prefix);
179179
}

lib/AST/RequirementMachine/PropertyMap.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class PropertyBag {
124124
Type getSuperclassBound(
125125
TypeArrayView<GenericTypeParamType> genericParams,
126126
const MutableTerm &lookupTerm,
127-
RewriteContext &ctx) const;
127+
const PropertyMap &map) const;
128128

129129
bool isConcreteType() const {
130130
return ConcreteType.hasValue();
@@ -137,7 +137,7 @@ class PropertyBag {
137137
Type getConcreteType(
138138
TypeArrayView<GenericTypeParamType> genericParams,
139139
const MutableTerm &lookupTerm,
140-
RewriteContext &ctx) const;
140+
const PropertyMap &map) const;
141141

142142
LayoutConstraint getLayoutConstraint() const {
143143
return Layout;
@@ -220,6 +220,31 @@ class PropertyMap {
220220

221221
void dump(llvm::raw_ostream &out) const;
222222

223+
/// Return the rewrite context used for allocating memory.
224+
RewriteContext &getRewriteContext() const { return Context; }
225+
226+
//////////////////////////////////////////////////////////////////////////////
227+
///
228+
/// Term to type conversion. The opposite direction is implemented in
229+
/// RewriteContext because it does not depend on the current rewrite system.
230+
///
231+
//////////////////////////////////////////////////////////////////////////////
232+
233+
Type getTypeForTerm(Term term,
234+
TypeArrayView<GenericTypeParamType> genericParams) const;
235+
236+
Type getTypeForTerm(const MutableTerm &term,
237+
TypeArrayView<GenericTypeParamType> genericParams) const;
238+
239+
Type getRelativeTypeForTerm(
240+
const MutableTerm &term, const MutableTerm &prefix) const;
241+
242+
Type getTypeFromSubstitutionSchema(
243+
Type schema,
244+
ArrayRef<Term> substitutions,
245+
TypeArrayView<GenericTypeParamType> genericParams,
246+
const MutableTerm &prefix) const;
247+
223248
private:
224249
void clear();
225250

lib/AST/RequirementMachine/RequirementMachineRequests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ RequirementMachine::buildRequirementsFromRules(
107107
// Convert a rewrite rule into a requirement.
108108
auto createRequirementFromRule = [&](const Rule &rule) {
109109
if (auto prop = rule.isPropertyRule()) {
110-
auto subjectType = Context.getTypeForTerm(rule.getRHS(), genericParams);
110+
auto subjectType = Map.getTypeForTerm(rule.getRHS(), genericParams);
111111

112112
switch (prop->getKind()) {
113113
case Symbol::Kind::Protocol:
@@ -125,7 +125,7 @@ RequirementMachine::buildRequirementsFromRules(
125125
case Symbol::Kind::Superclass: {
126126
// For compatibility with the old GenericSignatureBuilder, drop requirements
127127
// containing ErrorTypes.
128-
auto superclassType = Context.getTypeFromSubstitutionSchema(
128+
auto superclassType = Map.getTypeFromSubstitutionSchema(
129129
prop->getSuperclass(),
130130
prop->getSubstitutions(),
131131
genericParams, MutableTerm());
@@ -138,7 +138,7 @@ RequirementMachine::buildRequirementsFromRules(
138138
}
139139

140140
case Symbol::Kind::ConcreteType: {
141-
auto concreteType = Context.getTypeFromSubstitutionSchema(
141+
auto concreteType = Map.getTypeFromSubstitutionSchema(
142142
prop->getConcreteType(),
143143
prop->getSubstitutions(),
144144
genericParams, MutableTerm());
@@ -164,8 +164,8 @@ RequirementMachine::buildRequirementsFromRules(
164164
}
165165

166166
assert(rule.getLHS().back().getKind() != Symbol::Kind::Protocol);
167-
auto constraintType = Context.getTypeForTerm(rule.getLHS(), genericParams);
168-
auto subjectType = Context.getTypeForTerm(rule.getRHS(), genericParams);
167+
auto constraintType = Map.getTypeForTerm(rule.getLHS(), genericParams);
168+
auto subjectType = Map.getTypeForTerm(rule.getRHS(), genericParams);
169169

170170
sameTypeReqs[subjectType.getPointer()].Members.push_back(constraintType);
171171
};

lib/AST/RequirementMachine/RewriteContext.h

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ class RewriteContext final {
137137

138138
DebugOptions getDebugOptions() const { return Debug; }
139139

140+
ASTContext &getASTContext() const { return Context; }
141+
142+
//////////////////////////////////////////////////////////////////////////////
143+
///
144+
/// Reduction order on protocols.
145+
///
146+
//////////////////////////////////////////////////////////////////////////////
147+
140148
const llvm::TinyPtrVector<const ProtocolDecl *> &
141149
getInheritedProtocols(const ProtocolDecl *proto);
142150

@@ -147,35 +155,32 @@ class RewriteContext final {
147155
int compareProtocols(const ProtocolDecl *lhs,
148156
const ProtocolDecl *rhs);
149157

158+
//////////////////////////////////////////////////////////////////////////////
159+
///
160+
/// Type to term conversion. The opposite direction is implemented in
161+
/// PropertyMap because it depends on the current rewrite system.
162+
///
163+
//////////////////////////////////////////////////////////////////////////////
164+
150165
Term getTermForType(CanType paramType, const ProtocolDecl *proto);
151166

152167
MutableTerm getMutableTermForType(CanType paramType,
153168
const ProtocolDecl *proto);
154169

155-
ASTContext &getASTContext() const { return Context; }
156-
157-
Type getTypeForTerm(Term term,
158-
TypeArrayView<GenericTypeParamType> genericParams) const;
159-
160-
Type getTypeForTerm(const MutableTerm &term,
161-
TypeArrayView<GenericTypeParamType> genericParams) const;
162-
163-
Type getRelativeTypeForTerm(
164-
const MutableTerm &term, const MutableTerm &prefix) const;
165-
166170
MutableTerm getRelativeTermForType(CanType typeWitness,
167171
ArrayRef<Term> substitutions);
168172

169-
Type getTypeFromSubstitutionSchema(
170-
Type schema,
171-
ArrayRef<Term> substitutions,
172-
TypeArrayView<GenericTypeParamType> genericParams,
173-
const MutableTerm &prefix) const;
174-
175173
AssociatedTypeDecl *getAssociatedTypeForSymbol(Symbol symbol);
176174

177175
Symbol mergeAssociatedTypes(Symbol lhs, Symbol rhs);
178176

177+
//////////////////////////////////////////////////////////////////////////////
178+
///
179+
/// Construction of requirement machines for connected components in the
180+
/// protocol dependency graph.
181+
///
182+
//////////////////////////////////////////////////////////////////////////////
183+
179184
RequirementMachine *getRequirementMachine(CanGenericSignature sig);
180185
bool isRecursivelyConstructingRequirementMachine(CanGenericSignature sig);
181186

0 commit comments

Comments
 (0)