@@ -465,12 +465,8 @@ Type ConstraintSystem::openUnboundGenericType(UnboundGenericType *unbound,
465
465
}
466
466
467
467
// Open up the generic type.
468
- openGeneric (unboundDecl->getInnermostDeclContext (),
469
- unboundDecl->getDeclContext (),
470
- unboundDecl->getGenericSignature (),
471
- /* skipProtocolSelfConstraint=*/ false ,
472
- locator,
473
- replacements);
468
+ openGeneric (unboundDecl->getDeclContext (), unboundDecl->getGenericSignature (),
469
+ locator, replacements);
474
470
475
471
if (parentTy) {
476
472
auto subs = parentTy->getContextSubstitutions (
@@ -633,45 +629,25 @@ Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements) {
633
629
});
634
630
}
635
631
636
- Type ConstraintSystem::openFunctionType (
632
+ FunctionType * ConstraintSystem::openFunctionType (
637
633
AnyFunctionType *funcType,
638
- unsigned numArgumentLabelsToRemove,
639
634
ConstraintLocatorBuilder locator,
640
635
OpenedTypeMap &replacements,
641
- DeclContext *innerDC,
642
- DeclContext *outerDC,
643
- bool skipProtocolSelfConstraint,
644
- bool skipGenericRequirements) {
645
- Type type;
646
-
636
+ DeclContext *outerDC) {
647
637
if (auto *genericFn = funcType->getAs <GenericFunctionType>()) {
648
- // Open up the generic parameters and requirements.
649
- openGeneric (innerDC,
650
- outerDC,
651
- genericFn->getGenericSignature (),
652
- skipProtocolSelfConstraint,
653
- locator,
654
- replacements,
655
- skipGenericRequirements);
656
-
657
- // Transform the parameters and output type.
658
- llvm::SmallVector<AnyFunctionType::Param, 4 > openedParams;
659
- openedParams.reserve (genericFn->getNumParams ());
660
- for (const auto ¶m : genericFn->getParams ()) {
661
- auto type = openType (param.getPlainType (), replacements);
662
- openedParams.push_back (AnyFunctionType::Param (type, param.getLabel (),
663
- param.getParameterFlags ()));
664
- }
638
+ auto *signature = genericFn->getGenericSignature ();
665
639
666
- auto resultTy = openType (genericFn-> getResult (), replacements);
640
+ openGenericParameters (outerDC, signature, replacements, locator );
667
641
668
- // Build the resulting (non-generic) function type.
669
- funcType = FunctionType::get (
670
- openedParams, resultTy,
671
- FunctionType::ExtInfo ().withThrows (genericFn->throws ()));
642
+ openGenericRequirements (
643
+ outerDC, signature, /* skipProtocolSelfConstraint=*/ false , locator,
644
+ [&](Type type) -> Type { return openType (type, replacements); });
645
+
646
+ funcType = genericFn->substGenericArgs (
647
+ [&](Type type) { return openType (type, replacements); });
672
648
}
673
649
674
- return funcType->removeArgumentLabels (numArgumentLabelsToRemove );
650
+ return funcType->castTo <FunctionType>( );
675
651
}
676
652
677
653
Optional<Type> ConstraintSystem::isArrayType (Type type) {
@@ -934,14 +910,9 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
934
910
935
911
OpenedTypeMap replacements;
936
912
937
- auto openedType = openFunctionType (
938
- func->getInterfaceType ()->castTo <AnyFunctionType>(),
939
- /* numArgumentLabelsToRemove=*/ 0 ,
940
- locator, replacements,
941
- func->getInnermostDeclContext (),
942
- func->getDeclContext (),
943
- /* skipProtocolSelfConstraint=*/ false );
944
- auto openedFnType = openedType->castTo <FunctionType>();
913
+ auto openedType =
914
+ openFunctionType (func->getInterfaceType ()->castTo <AnyFunctionType>(),
915
+ locator, replacements, func->getDeclContext ());
945
916
946
917
// If we opened up any type variables, record the replacements.
947
918
recordOpenedTypes (locator, replacements);
@@ -950,36 +921,32 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
950
921
// DynamicSelf with the actual object type.
951
922
if (!func->getDeclContext ()->getSelfProtocolDecl ()) {
952
923
if (func->hasDynamicSelf ()) {
953
- auto params = openedFnType ->getParams ();
924
+ auto params = openedType ->getParams ();
954
925
assert (params.size () == 1 );
955
926
Type selfTy = params.front ().getPlainType ()->getMetatypeInstanceType ();
956
- openedType = openedType->replaceCovariantResultType (selfTy, 2 );
957
- openedFnType = openedType ->castTo <FunctionType>();
927
+ openedType = openedType->replaceCovariantResultType (selfTy, 2 )
928
+ ->castTo <FunctionType>();
958
929
}
959
930
} else {
960
- openedType = openedType->eraseDynamicSelfType ();
961
- openedFnType = openedType->castTo <FunctionType>();
931
+ openedType = openedType->eraseDynamicSelfType ()->castTo <FunctionType>();
962
932
}
963
933
964
934
// The reference implicitly binds 'self'.
965
- return { openedType, openedFnType ->getResult () };
935
+ return {openedType, openedType ->getResult ()};
966
936
}
967
937
968
938
// Unqualified reference to a local or global function.
969
939
if (auto funcDecl = dyn_cast<AbstractFunctionDecl>(value)) {
970
940
OpenedTypeMap replacements;
971
941
972
942
auto funcType = funcDecl->getInterfaceType ()->castTo <AnyFunctionType>();
973
- auto openedType =
974
- openFunctionType (
975
- funcType,
976
- getNumRemovedArgumentLabels (TC, funcDecl,
977
- /* isCurriedInstanceReference=*/ false ,
978
- functionRefKind),
979
- locator, replacements,
980
- funcDecl->getInnermostDeclContext (),
981
- funcDecl->getDeclContext (),
982
- /* skipProtocolSelfConstraint=*/ false );
943
+ auto numLabelsToRemove = getNumRemovedArgumentLabels (
944
+ TC, funcDecl,
945
+ /* isCurriedInstanceReference=*/ false , functionRefKind);
946
+
947
+ auto openedType = openFunctionType (funcType, locator, replacements,
948
+ funcDecl->getDeclContext ())
949
+ ->removeArgumentLabels (numLabelsToRemove);
983
950
984
951
// If we opened up any type variables, record the replacements.
985
952
recordOpenedTypes (locator, replacements);
@@ -1103,46 +1070,44 @@ static void bindArchetypesFromContext(
1103
1070
}
1104
1071
1105
1072
void ConstraintSystem::openGeneric (
1106
- DeclContext *innerDC,
1107
1073
DeclContext *outerDC,
1108
1074
GenericSignature *sig,
1109
- bool skipProtocolSelfConstraint,
1110
1075
ConstraintLocatorBuilder locator,
1111
- OpenedTypeMap &replacements,
1112
- bool skipGenericRequirements) {
1076
+ OpenedTypeMap &replacements) {
1113
1077
if (sig == nullptr )
1114
1078
return ;
1115
1079
1116
- auto locatorPtr = getConstraintLocator (locator);
1080
+ openGenericParameters (outerDC, sig, replacements, locator);
1081
+
1082
+ // Add the requirements as constraints.
1083
+ openGenericRequirements (
1084
+ outerDC, sig, /* skipProtocolSelfConstraint=*/ false , locator,
1085
+ [&](Type type) { return openType (type, replacements); });
1086
+ }
1087
+
1088
+ void ConstraintSystem::openGenericParameters (DeclContext *outerDC,
1089
+ GenericSignature *sig,
1090
+ OpenedTypeMap &replacements,
1091
+ ConstraintLocatorBuilder locator) {
1092
+ assert (sig);
1117
1093
1118
1094
// Create the type variables for the generic parameters.
1119
1095
for (auto gp : sig->getGenericParams ()) {
1120
- locatorPtr = getConstraintLocator (
1121
- locator.withPathElement (LocatorPathElt (gp)));
1122
-
1123
- auto typeVar = createTypeVariable (locatorPtr,
1124
- TVO_PrefersSubtypeBinding);
1125
- auto result = replacements.insert (
1126
- std::make_pair (cast<GenericTypeParamType>(gp->getCanonicalType ()),
1127
- typeVar));
1096
+ auto *paramLocator =
1097
+ getConstraintLocator (locator.withPathElement (LocatorPathElt (gp)));
1098
+
1099
+ auto typeVar = createTypeVariable (paramLocator, TVO_PrefersSubtypeBinding);
1100
+ auto result = replacements.insert (std::make_pair (
1101
+ cast<GenericTypeParamType>(gp->getCanonicalType ()), typeVar));
1102
+
1128
1103
assert (result.second );
1129
- (void ) result;
1104
+ (void )result;
1130
1105
}
1131
1106
1132
- // Remember that any new constraints generated by opening this generic are
1133
- // due to the opening.
1134
- locatorPtr = getConstraintLocator (
1107
+ auto *baseLocator = getConstraintLocator (
1135
1108
locator.withPathElement (LocatorPathElt::getOpenedGeneric (sig)));
1136
1109
1137
- bindArchetypesFromContext (*this , outerDC, locatorPtr, replacements);
1138
-
1139
- if (skipGenericRequirements)
1140
- return ;
1141
-
1142
- // Add the requirements as constraints.
1143
- openGenericRequirements (
1144
- outerDC, sig, skipProtocolSelfConstraint, locator,
1145
- [&](Type type) { return openType (type, replacements); });
1110
+ bindArchetypesFromContext (*this , outerDC, baseLocator, replacements);
1146
1111
}
1147
1112
1148
1113
void ConstraintSystem::openGenericRequirements (
@@ -1331,10 +1296,17 @@ ConstraintSystem::getTypeOfMemberReference(
1331
1296
1332
1297
// While opening member function type, let's delay opening requirements
1333
1298
// to allow contextual types to affect the situation.
1334
- openedType = openFunctionType (funcType, numRemovedArgumentLabels,
1335
- locator, replacements, innerDC, outerDC,
1336
- /* skipProtocolSelfConstraint=*/ true ,
1337
- /* skipGenericRequirements=*/ true );
1299
+ if (auto *genericFn = funcType->getAs <GenericFunctionType>()) {
1300
+ openGenericParameters (outerDC, genericFn->getGenericSignature (),
1301
+ replacements, locator);
1302
+
1303
+ openedType = genericFn->substGenericArgs (
1304
+ [&](Type type) { return openType (type, replacements); });
1305
+ } else {
1306
+ openedType = funcType;
1307
+ }
1308
+
1309
+ openedType = openedType->removeArgumentLabels (numRemovedArgumentLabels);
1338
1310
1339
1311
if (!outerDC->getSelfProtocolDecl ()) {
1340
1312
// Class methods returning Self as well as constructors get the
0 commit comments