Skip to content

Commit 148f638

Browse files
committed
[GSB] Basic infrastructure for delaying and reprocessing requirements.
1 parent 10ebdcd commit 148f638

File tree

2 files changed

+190
-51
lines changed

2 files changed

+190
-51
lines changed

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class GenericSignatureBuilder {
8282

8383
class FloatingRequirementSource;
8484

85+
class DelayedRequirement;
86+
8587
/// Describes a specific constraint on a potential archetype.
8688
template<typename T>
8789
struct Constraint {
@@ -190,6 +192,21 @@ class GenericSignatureBuilder {
190192
/// The constraint conflicted with existing constraints in some way;
191193
/// the generic signature is ill-formed.
192194
Conflicting,
195+
196+
/// The constraint could not be resolved immediately.
197+
Unresolved,
198+
};
199+
200+
/// Enum used to indicate how we should handle a constraint that cannot be
201+
/// processed immediately for some reason.
202+
enum class UnresolvedHandlingKind : char {
203+
/// Generate a new, unresolved constraint and consider the constraint
204+
/// "resolved" at this point.
205+
GenerateConstraints = 0,
206+
207+
/// Do not generate a new constraint; rather, return
208+
/// \c ConstraintResult::Unresolved and let the caller handle it.
209+
ReturnUnresolved = 1,
193210
};
194211

195212
private:
@@ -207,13 +224,15 @@ class GenericSignatureBuilder {
207224

208225
/// When a particular requirement cannot be resolved due to, e.g., a
209226
/// currently-unresolvable or nested type, this routine should be
210-
/// called to record the unresolved requirement to be reconsidered later.
227+
/// called to cope with the unresolved requirement.
211228
///
212-
/// \returns ConstraintResult::Resolved.
213-
ConstraintResult recordUnresolvedRequirement(RequirementKind kind,
229+
/// \returns \c ConstraintResult::Resolved or ConstraintResult::Delayed,
230+
/// as appropriate based on \c unresolvedHandling.
231+
ConstraintResult handleUnresolvedRequirement(RequirementKind kind,
214232
UnresolvedType lhs,
215233
RequirementRHS rhs,
216-
FloatingRequirementSource source);
234+
FloatingRequirementSource source,
235+
UnresolvedHandlingKind unresolvedHandling);
217236

218237
/// Retrieve the constraint source conformance for the superclass constraint
219238
/// of the given potential archetype (if present) to the given protocol.
@@ -265,9 +284,11 @@ class GenericSignatureBuilder {
265284
///
266285
/// The types are resolved with \c GenericSignatureBuilder::resolve, and must
267286
/// not be incompatible concrete types.
268-
ConstraintResult addSameTypeRequirement(UnresolvedType paOrT1,
269-
UnresolvedType paOrT2,
270-
FloatingRequirementSource Source);
287+
ConstraintResult addSameTypeRequirement(
288+
UnresolvedType paOrT1,
289+
UnresolvedType paOrT2,
290+
FloatingRequirementSource Source,
291+
UnresolvedHandlingKind unresolvedHandling);
271292

272293
/// \brief Add a new same-type requirement between two unresolved types.
273294
///
@@ -277,6 +298,7 @@ class GenericSignatureBuilder {
277298
ConstraintResult
278299
addSameTypeRequirement(UnresolvedType paOrT1, UnresolvedType paOrT2,
279300
FloatingRequirementSource Source,
301+
UnresolvedHandlingKind unresolvedHandling,
280302
llvm::function_ref<void(Type, Type)> diagnoseMismatch);
281303

282304
/// Update the superclass for the equivalence class of \c T.
@@ -300,6 +322,7 @@ class GenericSignatureBuilder {
300322
UnresolvedType subject,
301323
UnresolvedType constraint,
302324
FloatingRequirementSource source,
325+
UnresolvedHandlingKind unresolvedHandling,
303326
llvm::SmallPtrSetImpl<ProtocolDecl *> *visited
304327
= nullptr);
305328

@@ -334,9 +357,11 @@ class GenericSignatureBuilder {
334357
const RequirementSource *Source);
335358

336359
/// Add a new layout requirement to the subject.
337-
ConstraintResult addLayoutRequirement(UnresolvedType subject,
338-
LayoutConstraint layout,
339-
FloatingRequirementSource source);
360+
ConstraintResult addLayoutRequirement(
361+
UnresolvedType subject,
362+
LayoutConstraint layout,
363+
FloatingRequirementSource source,
364+
UnresolvedHandlingKind unresolvedHandling);
340365

341366
/// Add the requirements placed on the given type parameter
342367
/// to the given potential archetype.
@@ -480,6 +505,9 @@ class GenericSignatureBuilder {
480505
ArrayRef<GenericTypeParamType *> genericParams);
481506

482507
private:
508+
/// Process any delayed requirements that can be handled now.
509+
void processDelayedRequirements();
510+
483511
/// Describes the relationship between a given constraint and
484512
/// the canonical constraint of the equivalence class.
485513
enum class ConstraintRelation {
@@ -1545,6 +1573,15 @@ class GenericSignatureBuilder::PotentialArchetype {
15451573
friend class GenericSignatureBuilder;
15461574
};
15471575

1576+
/// Describes a requirement whose processing has been delayed for some reason.
1577+
class GenericSignatureBuilder::DelayedRequirement {
1578+
public:
1579+
RequirementKind kind;
1580+
UnresolvedType lhs;
1581+
RequirementRHS rhs;
1582+
FloatingRequirementSource source;
1583+
};
1584+
15481585
/// Whether the given constraint result signals an error.
15491586
inline bool isErrorResult(GenericSignatureBuilder::ConstraintResult result) {
15501587
switch (result) {
@@ -1553,6 +1590,7 @@ inline bool isErrorResult(GenericSignatureBuilder::ConstraintResult result) {
15531590
return true;
15541591

15551592
case GenericSignatureBuilder::ConstraintResult::Resolved:
1593+
case GenericSignatureBuilder::ConstraintResult::Unresolved:
15561594
return false;
15571595
}
15581596
}

0 commit comments

Comments
 (0)