23
23
#include " swift/AST/Decl.h"
24
24
#include " swift/AST/DiagnosticEngine.h"
25
25
#include " swift/AST/Identifier.h"
26
+ #include " swift/AST/ProtocolConformanceRef.h"
26
27
#include " swift/AST/Types.h"
27
28
#include " swift/AST/TypeLoc.h"
28
29
#include " swift/AST/TypeRepr.h"
@@ -74,6 +75,11 @@ enum class ArchetypeResolutionKind {
74
75
// / Only create a new potential archetype to describe this dependent type
75
76
// / if it is already known.
76
77
AlreadyKnown,
78
+
79
+ // / Only create a potential archetype when it is well-formed (i.e., we know
80
+ // / that there is a nested type with that name), but (unlike \c AlreadyKnown)
81
+ // / allow the creation of a new potential archetype.
82
+ WellFormed,
77
83
};
78
84
79
85
// / \brief Collects a set of requirements of generic parameters, both explicitly
@@ -176,6 +182,9 @@ class GenericSignatureBuilder {
176
182
// / the concrete type.
177
183
unsigned recursiveConcreteType : 1 ;
178
184
185
+ // / Whether we have an invalid concrete type.
186
+ unsigned invalidConcreteType : 1 ;
187
+
179
188
// / Whether we have detected recursion during the substitution of
180
189
// / the superclass type.
181
190
unsigned recursiveSuperclassType : 1 ;
@@ -281,16 +290,24 @@ class GenericSignatureBuilder {
281
290
FloatingRequirementSource source,
282
291
UnresolvedHandlingKind unresolvedHandling);
283
292
293
+ // / Resolve the conformance of the given potential archetype to
294
+ // / the given protocol when the potential archetype is known to be equivalent
295
+ // / to a concrete type.
296
+ // /
297
+ // / \returns the requirement source for the resolved conformance, or nullptr
298
+ // / if the conformance could not be resolved.
299
+ const RequirementSource *resolveConcreteConformance (PotentialArchetype *pa,
300
+ ProtocolDecl *proto);
301
+
284
302
// / Retrieve the constraint source conformance for the superclass constraint
285
303
// / of the given potential archetype (if present) to the given protocol.
286
304
// /
287
305
// / \param pa The potential archetype whose superclass constraint is being
288
306
// / queried.
289
307
// /
290
308
// / \param proto The protocol to which we are establishing conformance.
291
- const RequirementSource *resolveSuperConformance (
292
- GenericSignatureBuilder::PotentialArchetype *pa,
293
- ProtocolDecl *proto);
309
+ const RequirementSource *resolveSuperConformance (PotentialArchetype *pa,
310
+ ProtocolDecl *proto);
294
311
295
312
// / \brief Add a new conformance requirement specifying that the given
296
313
// / potential archetype conforms to the given protocol.
@@ -316,15 +333,6 @@ class GenericSignatureBuilder {
316
333
FloatingRequirementSource Source,
317
334
llvm::function_ref<void (Type, Type)> diagnoseMismatch);
318
335
319
- // / \brief Add a new same-type requirement between two fully resolved types
320
- // / (output of GenericSignatureBuilder::resolve).
321
- // /
322
- // / The two types must not be incompatible concrete types.
323
- ConstraintResult addSameTypeRequirementDirect (
324
- ResolvedType paOrT1,
325
- ResolvedType paOrT2,
326
- FloatingRequirementSource Source);
327
-
328
336
// / \brief Add a new same-type requirement between two unresolved types.
329
337
// /
330
338
// / The types are resolved with \c GenericSignatureBuilder::resolve, and must
@@ -770,7 +778,7 @@ class GenericSignatureBuilder::RequirementSource final
770
778
771
779
// / A requirement that was resolved via a superclass requirement.
772
780
// /
773
- // / This stores the \c ProtocolConformance* used to resolve the
781
+ // / This stores the \c ProtocolConformanceRef used to resolve the
774
782
// / requirement.
775
783
Superclass,
776
784
@@ -784,6 +792,10 @@ class GenericSignatureBuilder::RequirementSource final
784
792
// / This stores the \c ProtocolConformance* used to resolve the
785
793
// / requirement.
786
794
Concrete,
795
+
796
+ // / A requirement that was resolved based on structural derivation from
797
+ // / another requirement.
798
+ Derived,
787
799
};
788
800
789
801
// / The kind of requirement source.
@@ -792,6 +804,7 @@ class GenericSignatureBuilder::RequirementSource final
792
804
private:
793
805
// / The kind of storage we have.
794
806
enum class StorageKind : uint8_t {
807
+ None,
795
808
RootArchetype,
796
809
StoredType,
797
810
ProtocolConformance,
@@ -816,7 +829,7 @@ class GenericSignatureBuilder::RequirementSource final
816
829
TypeBase *type;
817
830
818
831
// / A protocol conformance used to satisfy the requirement.
819
- ProtocolConformance *conformance;
832
+ void *conformance;
820
833
821
834
// / An associated type to which a requirement is being applied.
822
835
AssociatedTypeDecl *assocType;
@@ -839,6 +852,7 @@ class GenericSignatureBuilder::RequirementSource final
839
852
case Superclass:
840
853
case Parent:
841
854
case Concrete:
855
+ case Derived:
842
856
return 0 ;
843
857
}
844
858
@@ -882,6 +896,7 @@ class GenericSignatureBuilder::RequirementSource final
882
896
case Superclass:
883
897
case Parent:
884
898
case Concrete:
899
+ case Derived:
885
900
return false ;
886
901
}
887
902
@@ -931,7 +946,7 @@ class GenericSignatureBuilder::RequirementSource final
931
946
}
932
947
933
948
RequirementSource (Kind kind, const RequirementSource *parent,
934
- ProtocolConformance * conformance)
949
+ ProtocolConformanceRef conformance)
935
950
: kind(kind), storageKind(StorageKind::ProtocolConformance),
936
951
hasTrailingWrittenRequirementLoc(false ),
937
952
usesRequirementSignature(false ), parent(parent) {
@@ -940,7 +955,7 @@ class GenericSignatureBuilder::RequirementSource final
940
955
assert (isAcceptableStorageKind (kind, storageKind) &&
941
956
" RequirementSource kind/storageKind mismatch" );
942
957
943
- storage.conformance = conformance;
958
+ storage.conformance = conformance. getOpaqueValue () ;
944
959
}
945
960
946
961
RequirementSource (Kind kind, const RequirementSource *parent,
@@ -956,6 +971,16 @@ class GenericSignatureBuilder::RequirementSource final
956
971
storage.assocType = assocType;
957
972
}
958
973
974
+ RequirementSource (Kind kind, const RequirementSource *parent)
975
+ : kind(kind), storageKind(StorageKind::None),
976
+ hasTrailingWrittenRequirementLoc(false ),
977
+ usesRequirementSignature(false ), parent(parent) {
978
+ assert ((static_cast <bool >(parent) != isRootKind (kind)) &&
979
+ " Root RequirementSource should not have parent (or vice versa)" );
980
+ assert (isAcceptableStorageKind (kind, storageKind) &&
981
+ " RequirementSource kind/storageKind mismatch" );
982
+ }
983
+
959
984
public:
960
985
// / Retrieve an abstract requirement source.
961
986
static const RequirementSource *forAbstract (PotentialArchetype *root);
@@ -997,13 +1022,14 @@ class GenericSignatureBuilder::RequirementSource final
997
1022
// / A requirement source that describes that a requirement that is resolved
998
1023
// / via a superclass requirement.
999
1024
const RequirementSource *viaSuperclass (
1000
- GenericSignatureBuilder &builder,
1001
- ProtocolConformance * conformance) const ;
1025
+ GenericSignatureBuilder &builder,
1026
+ ProtocolConformanceRef conformance) const ;
1002
1027
1003
1028
// / A requirement source that describes that a requirement that is resolved
1004
1029
// / via a same-type-to-concrete requirement.
1005
- const RequirementSource *viaConcrete (GenericSignatureBuilder &builder,
1006
- ProtocolConformance *conformance) const ;
1030
+ const RequirementSource *viaConcrete (
1031
+ GenericSignatureBuilder &builder,
1032
+ ProtocolConformanceRef conformance) const ;
1007
1033
1008
1034
// / A constraint source that describes that a constraint that is resolved
1009
1035
// / for a nested type via a constraint on its parent.
@@ -1012,6 +1038,10 @@ class GenericSignatureBuilder::RequirementSource final
1012
1038
const RequirementSource *viaParent (GenericSignatureBuilder &builder,
1013
1039
AssociatedTypeDecl *assocType) const ;
1014
1040
1041
+ // / A constraint source that describes a constraint that is structurally
1042
+ // / derived from another constraint but does not require further information.
1043
+ const RequirementSource *viaDerived (GenericSignatureBuilder &builder) const ;
1044
+
1015
1045
// / Retrieve the root requirement source.
1016
1046
const RequirementSource *getRoot () const ;
1017
1047
@@ -1100,9 +1130,9 @@ class GenericSignatureBuilder::RequirementSource final
1100
1130
ProtocolDecl *getProtocolDecl () const ;
1101
1131
1102
1132
// / Retrieve the protocol conformance for this requirement, if there is one.
1103
- ProtocolConformance * getProtocolConformance () const {
1104
- if (storageKind != StorageKind::ProtocolConformance) return nullptr ;
1105
- return storage.conformance ;
1133
+ ProtocolConformanceRef getProtocolConformance () const {
1134
+ assert (storageKind == StorageKind::ProtocolConformance);
1135
+ return ProtocolConformanceRef::getFromOpaqueValue ( storage.conformance ) ;
1106
1136
}
1107
1137
1108
1138
// / Retrieve the associated type declaration for this requirement, if there
@@ -1577,18 +1607,6 @@ class GenericSignatureBuilder::PotentialArchetype {
1577
1607
PotentialArchetype *getNestedType (TypeDecl *concreteDecl,
1578
1608
GenericSignatureBuilder &builder);
1579
1609
1580
- // / Describes the kind of update that is performed.
1581
- enum class NestedTypeUpdate {
1582
- // / Resolve an existing potential archetype, but don't create a new
1583
- // / one if not present.
1584
- ResolveExisting,
1585
- // / If this potential archetype is missing, create it.
1586
- AddIfMissing,
1587
- // / If this potential archetype is missing and would be a better anchor,
1588
- // / create it.
1589
- AddIfBetterAnchor,
1590
- };
1591
-
1592
1610
// / \brief Retrieve (or create) a nested type that is the current best
1593
1611
// / nested archetype anchor (locally) with the given name.
1594
1612
// /
@@ -1597,7 +1615,7 @@ class GenericSignatureBuilder::PotentialArchetype {
1597
1615
PotentialArchetype *getNestedArchetypeAnchor (
1598
1616
Identifier name,
1599
1617
GenericSignatureBuilder &builder,
1600
- NestedTypeUpdate kind = NestedTypeUpdate::AddIfMissing );
1618
+ ArchetypeResolutionKind kind);
1601
1619
1602
1620
// / Update the named nested type when we know this type conforms to the given
1603
1621
// / protocol.
@@ -1607,7 +1625,7 @@ class GenericSignatureBuilder::PotentialArchetype {
1607
1625
// / a potential archetype should not be created if it's missing.
1608
1626
PotentialArchetype *updateNestedTypeForConformance (
1609
1627
PointerUnion<AssociatedTypeDecl *, TypeDecl *> type,
1610
- NestedTypeUpdate kind);
1628
+ ArchetypeResolutionKind kind);
1611
1629
1612
1630
// / Update the named nested type when we know this type conforms to the given
1613
1631
// / protocol.
@@ -1618,7 +1636,7 @@ class GenericSignatureBuilder::PotentialArchetype {
1618
1636
PotentialArchetype *updateNestedTypeForConformance (
1619
1637
Identifier name,
1620
1638
ProtocolDecl *protocol,
1621
- NestedTypeUpdate kind);
1639
+ ArchetypeResolutionKind kind);
1622
1640
1623
1641
// / \brief Retrieve (or build) the type corresponding to the potential
1624
1642
// / archetype within the given generic environment.
0 commit comments