@@ -1214,6 +1214,78 @@ AbstractionPattern AbstractionPattern::getFunctionResultType() const {
12141214 llvm_unreachable (" bad kind" );
12151215}
12161216
1217+ llvm::Optional<AbstractionPattern>
1218+ AbstractionPattern::getFunctionThrownErrorType () const {
1219+ switch (getKind ()) {
1220+ case Kind::Invalid:
1221+ llvm_unreachable (" querying invalid abstraction pattern!" );
1222+ case Kind::ObjCCompletionHandlerArgumentsType:
1223+ case Kind::Tuple:
1224+ llvm_unreachable (" abstraction pattern for tuple cannot be function" );
1225+ case Kind::Opaque:
1226+ return *this ;
1227+ case Kind::Type: {
1228+ if (isTypeParameterOrOpaqueArchetype ())
1229+ return getOpaque ();
1230+
1231+ if (auto errorType = cast<AnyFunctionType>(getType ())->getEffectiveThrownErrorType ()) {
1232+ return AbstractionPattern (getGenericSubstitutions (),
1233+ getGenericSignatureForFunctionComponent (),
1234+ (*errorType)->getCanonicalType ());
1235+ }
1236+
1237+ return llvm::None;
1238+ }
1239+ case Kind::Discard:
1240+ llvm_unreachable (" don't need to discard function abstractions yet" );
1241+ case Kind::ClangType:
1242+ case Kind::CFunctionAsMethodType:
1243+ case Kind::PartialCurriedCFunctionAsMethodType:
1244+ case Kind::CXXMethodType:
1245+ case Kind::PartialCurriedCXXMethodType:
1246+ case Kind::CurriedObjCMethodType:
1247+ case Kind::CurriedCFunctionAsMethodType:
1248+ case Kind::CurriedCXXMethodType:
1249+ case Kind::PartialCurriedObjCMethodType:
1250+ case Kind::ObjCMethodType:
1251+ llvm_unreachable (" implement me" );
1252+ case Kind::OpaqueFunction:
1253+ case Kind::OpaqueDerivativeFunction:
1254+ return llvm::None;
1255+ }
1256+ llvm_unreachable (" bad kind" );
1257+ }
1258+
1259+ llvm::Optional<std::pair<AbstractionPattern, CanType>>
1260+ AbstractionPattern::getFunctionThrownErrorType (
1261+ CanAnyFunctionType substFnInterfaceType) const {
1262+ auto optOrigErrorType = getFunctionThrownErrorType ();
1263+ if (!optOrigErrorType)
1264+ return llvm::None;
1265+
1266+ auto &ctx = substFnInterfaceType->getASTContext ();
1267+ auto optErrorType = substFnInterfaceType->getEffectiveThrownErrorType ();
1268+
1269+ if (isTypeParameterOrOpaqueArchetype ()) {
1270+ if (!optErrorType)
1271+ return llvm::None;
1272+
1273+ if (!(*optErrorType)->isEqual (ctx.getErrorExistentialType ())) {
1274+ llvm::errs () << " unsupported reabstraction\n " ;
1275+ abort ();
1276+ }
1277+
1278+ return std::make_pair (AbstractionPattern (*optErrorType),
1279+ (*optErrorType)->getCanonicalType ());
1280+ }
1281+
1282+ if (!optErrorType)
1283+ optErrorType = ctx.getErrorExistentialType ();
1284+
1285+ return std::make_pair (*optOrigErrorType,
1286+ (*optErrorType)->getCanonicalType ());
1287+ }
1288+
12171289AbstractionPattern
12181290AbstractionPattern::getObjCMethodAsyncCompletionHandlerType (
12191291 CanType swiftCompletionHandlerType) const {
@@ -1939,7 +2011,7 @@ AbstractionPattern::getParameterConvention(TypeConverter &TC) const {
19392011 case Kind::Opaque:
19402012 // Maximally abstracted values are always passed indirectly.
19412013 return Indirect;
1942-
2014+
19432015 case Kind::OpaqueFunction:
19442016 case Kind::OpaqueDerivativeFunction:
19452017 case Kind::PartialCurriedObjCMethodType:
@@ -1953,16 +2025,57 @@ AbstractionPattern::getParameterConvention(TypeConverter &TC) const {
19532025 case Kind::PartialCurriedCXXMethodType:
19542026 // Function types are always passed directly
19552027 return Direct;
1956-
2028+
19572029 case Kind::ClangType:
19582030 case Kind::Type:
19592031 case Kind::Discard:
19602032 // Pass according to the formal type.
19612033 return SILType::isFormallyPassedIndirectly (getType (),
1962- TC,
1963- getGenericSignatureOrNull ())
2034+ TC,
2035+ getGenericSignatureOrNull ())
19642036 ? Indirect : Direct;
1965-
2037+
2038+ case Kind::Invalid:
2039+ case Kind::Tuple:
2040+ case Kind::ObjCCompletionHandlerArgumentsType:
2041+ llvm_unreachable (" should not get here" );
2042+ }
2043+ }
2044+
2045+ AbstractionPattern::CallingConventionKind
2046+ AbstractionPattern::getErrorConvention (TypeConverter &TC) const {
2047+ // Tuples should be destructured.
2048+ if (isTuple ()) {
2049+ return Destructured;
2050+ }
2051+ switch (getKind ()) {
2052+ case Kind::Opaque:
2053+ // Maximally abstracted values are always thrown indirectly.
2054+ return Indirect;
2055+
2056+ case Kind::OpaqueFunction:
2057+ case Kind::OpaqueDerivativeFunction:
2058+ case Kind::PartialCurriedObjCMethodType:
2059+ case Kind::CurriedObjCMethodType:
2060+ case Kind::PartialCurriedCFunctionAsMethodType:
2061+ case Kind::CurriedCFunctionAsMethodType:
2062+ case Kind::CFunctionAsMethodType:
2063+ case Kind::ObjCMethodType:
2064+ case Kind::CXXMethodType:
2065+ case Kind::CurriedCXXMethodType:
2066+ case Kind::PartialCurriedCXXMethodType:
2067+ // Function types are always thrown directly
2068+ return Direct;
2069+
2070+ case Kind::ClangType:
2071+ case Kind::Type:
2072+ case Kind::Discard:
2073+ // Pass according to the formal type.
2074+ return SILType::isFormallyThrownIndirectly (getType (),
2075+ TC,
2076+ getGenericSignatureOrNull ())
2077+ ? Indirect : Direct;
2078+
19662079 case Kind::Invalid:
19672080 case Kind::Tuple:
19682081 case Kind::ObjCCompletionHandlerArgumentsType:
@@ -2514,14 +2627,28 @@ class SubstFunctionTypePatternVisitor
25142627 if (yieldType) {
25152628 substYieldType = visit (yieldType, yieldPattern);
25162629 }
2517-
2630+
2631+ CanType newErrorType;
2632+
2633+ if (auto optPair = pattern.getFunctionThrownErrorType (func)) {
2634+ auto errorPattern = optPair->first ;
2635+ auto errorType = optPair->second ;
2636+ newErrorType = visit (errorType, errorPattern);
2637+ }
2638+
25182639 auto newResultTy = visit (func.getResult (),
25192640 pattern.getFunctionResultType ());
25202641
25212642 llvm::Optional<FunctionType::ExtInfo> extInfo;
25222643 if (func->hasExtInfo ())
25232644 extInfo = func->getExtInfo ();
2524-
2645+
2646+ if (newErrorType) {
2647+ if (!extInfo)
2648+ extInfo = FunctionType::ExtInfo ();
2649+ extInfo = extInfo->withThrows (true , newErrorType);
2650+ }
2651+
25252652 return CanFunctionType::get (FunctionType::CanParamArrayRef (newParams),
25262653 newResultTy, extInfo);
25272654 }
@@ -2562,7 +2689,7 @@ const {
25622689 auto substTy = visitor.handleUnabstractedFunctionType (substType, *this ,
25632690 substYieldType,
25642691 origYieldType);
2565-
2692+
25662693 auto substSig = buildGenericSignature (TC.Context , GenericSignature (),
25672694 std::move (visitor.substGenericParams ),
25682695 std::move (visitor.substRequirements ))
0 commit comments