Skip to content

Commit f44d1f7

Browse files
authored
Merge pull request #38147 from slavapestov/requirement-machine-queries
RequirementMachine: Implement some generic signature queries
2 parents 39e92db + 8ffe44f commit f44d1f7

17 files changed

+1424
-218
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace swift {
3333
class GenericSignatureBuilder;
3434
class ProtocolConformanceRef;
3535
class ProtocolType;
36+
class RequirementMachine;
3637
class SubstitutionMap;
3738
class GenericEnvironment;
3839

@@ -303,6 +304,9 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
303304
/// Retrieve the generic signature builder for the given generic signature.
304305
GenericSignatureBuilder *getGenericSignatureBuilder() const;
305306

307+
/// Retrieve the requirement machine for the given generic signature.
308+
RequirementMachine *getRequirementMachine() const;
309+
306310
/// Returns the generic environment that provides fresh contextual types
307311
/// (archetypes) that correspond to the interface types in this generic
308312
/// signature.

include/swift/AST/RequirementMachine.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@
1313
#ifndef SWIFT_REQUIREMENTMACHINE_H
1414
#define SWIFT_REQUIREMENTMACHINE_H
1515

16+
#include "swift/AST/GenericSignature.h"
17+
18+
namespace llvm {
19+
class raw_ostream;
20+
}
21+
1622
namespace swift {
1723

1824
class ASTContext;
1925
class AssociatedTypeDecl;
20-
class CanGenericSignature;
2126
class CanType;
22-
class GenericSignature;
27+
class LayoutConstraint;
2328
class ProtocolDecl;
2429
class Requirement;
30+
class Type;
2531

2632
/// Wraps a rewrite system with higher-level operations in terms of
2733
/// generic signatures and interface types.
@@ -43,10 +49,20 @@ class RequirementMachine final {
4349
void addGenericSignature(CanGenericSignature sig);
4450

4551
bool isComplete() const;
46-
void markComplete();
52+
void computeCompletion(CanGenericSignature sig);
4753

4854
public:
4955
~RequirementMachine();
56+
57+
// Generic signature queries
58+
bool requiresClass(Type depType) const;
59+
LayoutConstraint getLayoutConstraint(Type depType) const;
60+
bool requiresProtocol(Type depType, const ProtocolDecl *proto) const;
61+
GenericSignature::RequiredProtocols getRequiredProtocols(Type depType) const;
62+
bool isConcreteType(Type depType) const;
63+
bool areSameTypeParameterInContext(Type depType1, Type depType2) const;
64+
65+
void dump(llvm::raw_ostream &out) const;
5066
};
5167

5268
} // end namespace swift

include/swift/AST/TypeMatcher.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,21 @@ class TypeMatcher {
195195
TRIVIAL_CASE(ModuleType)
196196
TRIVIAL_CASE(DynamicSelfType)
197197
TRIVIAL_CASE(ArchetypeType)
198-
TRIVIAL_CASE(GenericTypeParamType)
199198
TRIVIAL_CASE(DependentMemberType)
200199

200+
bool visitGenericTypeParamType(CanGenericTypeParamType firstType,
201+
Type secondType,
202+
Type sugaredFirstType) {
203+
/* If the types match, continue. */
204+
if (!Matcher.asDerived().alwaysMismatchGenericParams() &&
205+
firstType->isEqual(secondType))
206+
return true;
207+
208+
/* Otherwise, let the derived class deal with the mismatch. */
209+
return mismatch(firstType.getPointer(), secondType,
210+
sugaredFirstType);
211+
}
212+
201213
/// FIXME: Split this out into cases?
202214
bool visitAnyFunctionType(CanAnyFunctionType firstFunc, Type secondType,
203215
Type sugaredFirstType) {
@@ -300,6 +312,8 @@ class TypeMatcher {
300312
#undef TRIVIAL_CASE
301313
};
302314

315+
bool alwaysMismatchGenericParams() const { return false; }
316+
303317
ImplClass &asDerived() { return static_cast<ImplClass &>(*this); }
304318

305319
const ImplClass &asDerived() const {

include/swift/Basic/LangOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ namespace swift {
453453

454454
/// Maximum iteration count for requirement machine confluent completion
455455
/// algorithm.
456-
unsigned RequirementMachineStepLimit = 1000;
456+
unsigned RequirementMachineStepLimit = 2000;
457457

458458
/// Maximum term length for requirement machine confluent completion
459459
/// algorithm.

include/swift/Basic/Statistics.def

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,12 @@ FRONTEND_STATISTIC(Sema, NumAccessorBodiesSynthesized)
224224
/// amount of work the requirement machine does analyzing type signatures.
225225
FRONTEND_STATISTIC(Sema, NumRequirementMachines)
226226

227-
/// Number of requirement machines constructed. Rough proxy for
228-
/// amount of work the requirement machine does analyzing type signatures.
227+
/// Number of new rules added by Knuth-Bendix completion procedure.
229228
FRONTEND_STATISTIC(Sema, NumRequirementMachineCompletionSteps)
230229

230+
/// Number of new rules added by concrete term unification.
231+
FRONTEND_STATISTIC(Sema, NumRequirementMachineUnifiedConcreteTerms)
232+
231233
/// Number of generic signature builders constructed. Rough proxy for
232234
/// amount of work the GSB does analyzing type signatures.
233235
FRONTEND_STATISTIC(Sema, NumGenericSignatureBuilders)

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ add_swift_host_library(swiftAST STATIC
7373
ProtocolConformance.cpp
7474
RawComment.cpp
7575
RequirementEnvironment.cpp
76+
RequirementMachine/EquivalenceClassMap.cpp
7677
RequirementMachine/ProtocolGraph.cpp
7778
RequirementMachine/RequirementMachine.cpp
7879
RequirementMachine/RewriteSystem.cpp

0 commit comments

Comments
 (0)