|
29 | 29 | #include "swift/SIL/SILArgument.h" |
30 | 30 | #include "swift/SIL/SILUndef.h" |
31 | 31 | #include "swift/SIL/TypeLowering.h" |
| 32 | +#include "clang/AST/DeclObjC.h" |
32 | 33 |
|
33 | 34 | using namespace swift; |
34 | 35 | using namespace Lowering; |
@@ -2022,25 +2023,30 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) { |
2022 | 2023 | B.createReturn(loc, result); |
2023 | 2024 | } |
2024 | 2025 |
|
2025 | | -static SILValue |
2026 | | -getThunkedForeignFunctionRef(SILGenFunction &SGF, |
2027 | | - SILLocation loc, |
2028 | | - SILDeclRef foreign, |
2029 | | - ArrayRef<ManagedValue> args, |
2030 | | - const SILConstantInfo &foreignCI) { |
| 2026 | +static SILValue getThunkedForeignFunctionRef(SILGenFunction &SGF, |
| 2027 | + AbstractFunctionDecl *fd, |
| 2028 | + SILDeclRef foreign, |
| 2029 | + ArrayRef<ManagedValue> args, |
| 2030 | + const SILConstantInfo &foreignCI) { |
2031 | 2031 | assert(foreign.isForeign); |
2032 | 2032 |
|
2033 | 2033 | // Produce an objc_method when thunking ObjC methods. |
2034 | | - if (foreignCI.SILFnType->getRepresentation() |
2035 | | - == SILFunctionTypeRepresentation::ObjCMethod) { |
2036 | | - SILValue thisArg = args.back().getValue(); |
| 2034 | + if (foreignCI.SILFnType->getRepresentation() == |
| 2035 | + SILFunctionTypeRepresentation::ObjCMethod) { |
| 2036 | + auto *objcDecl = |
| 2037 | + dyn_cast_or_null<clang::ObjCMethodDecl>(fd->getClangDecl()); |
| 2038 | + const bool isObjCDirect = objcDecl && objcDecl->isDirectMethod(); |
| 2039 | + if (isObjCDirect) { |
| 2040 | + auto *fn = SGF.SGM.getFunction(foreign, NotForDefinition); |
| 2041 | + return SGF.B.createFunctionRef(fd, fn); |
| 2042 | + } |
2037 | 2043 |
|
2038 | | - return SGF.B.createObjCMethod(loc, thisArg, foreign, |
2039 | | - foreignCI.getSILType()); |
| 2044 | + SILValue thisArg = args.back().getValue(); |
| 2045 | + return SGF.B.createObjCMethod(fd, thisArg, foreign, foreignCI.getSILType()); |
2040 | 2046 | } |
2041 | 2047 |
|
2042 | 2048 | // Otherwise, emit a function_ref. |
2043 | | - return SGF.emitGlobalFunctionRef(loc, foreign); |
| 2049 | + return SGF.emitGlobalFunctionRef(fd, foreign); |
2044 | 2050 | } |
2045 | 2051 |
|
2046 | 2052 | /// Generate code to emit a thunk with native conventions that calls a |
|
0 commit comments