[KN] CInterop: __attribute__((objc_direct)) support

Merge-request: KT-MR-8828
Merged-by: Gleb Lukianets <Gleb.Lukianets@jetbrains.com>
This commit is contained in:
Gleb Lukianets
2023-03-02 10:36:53 +00:00
committed by Space Team
parent ae07d0e9ce
commit fc96eb6d8d
17 changed files with 319 additions and 36 deletions
@@ -4379,6 +4379,13 @@ if (PlatformInfo.isAppleTarget(project)) {
it.headers "$projectDir/interop/objc/msg_send/messaging.h"
}
createInterop("objcDirect") {
it.defFile 'interop/objc/direct/direct.def'
it.headers "$projectDir/interop/objc/direct/direct.h"
it.extraOpts "-Xcompile-source", "$projectDir/interop/objc/direct/direct.m", "-Xsource-compiler-option", "-DNS_FORMAT_ARGUMENT(A)="
it.compilerOpts '-DNS_FORMAT_ARGUMENT(A)='
}
createInterop("foreignException") {
it.defFile 'interop/objc/foreignException/objc_wrap.def'
it.headers "$projectDir/interop/objc/foreignException/objc_wrap.h"
@@ -5081,6 +5088,11 @@ if (PlatformInfo.isAppleTarget(project)) {
}
}
interopTest("interop_objc_direct") {
source = "interop/objc/direct/main.kt"
interop = "objcDirect"
}
interopTest("interop_objc_foreignException") {
disabled = isK2(project) // KT-55909
source = "interop/objc/foreignException/objc_wrap.kt"
@@ -6583,6 +6595,15 @@ fileCheckTest("filecheck_constants_merge") {
annotatedSource = project.file('filecheck/constants_merge.kt')
}
fileCheckTest("filecheck_objc_direct") {
enabled = enabled && cacheTesting == null
annotatedSource = project.file('filecheck/objc_direct.kt')
interop = "objcDirect"
enabled = target.family.appleFamily
}
dependencies {
nopPluginApi kotlinCompilerModule
nopPluginApi project(":native:kotlin-native-utils")
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
import direct.*
import kotlinx.cinterop.*
//CHECK-LABEL: kfun:#callDirect(){}kotlin.ULong
fun callDirect(): ULong {
val cc = CallingConventions()
//CHECK: invoke i64 @_{{[a-zA-Z0-9]+}}_knbridge{{[0-9]+}}(i8* %{{[0-9]+}}, i64 42)
return cc.direct(42)
}
//CHECK-LABEL: kfun:#callRegular(){}kotlin.ULong
fun callRegular(): ULong {
val cc = CallingConventions()
//CHECK: invoke i64 @_{{[a-zA-Z0-9]+}}_knbridge{{[0-9]+}}(i8* %{{[0-9]+}}, i8* %{{[0-9]+}}, i64 42)
return cc.regular(42)
}
fun main() {
callDirect()
callRegular()
}
@@ -0,0 +1,2 @@
language = Objective-C
headerFilter = **/direct.h
@@ -0,0 +1,32 @@
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
__attribute__((objc_runtime_name("CC")))
__attribute__((swift_name("CC")))
@interface CallingConventions : NSObject
+ (NSUInteger)regular:(NSUInteger)arg;
- (NSUInteger)regular:(NSUInteger)arg;
+ (NSUInteger)direct:(NSUInteger)arg __attribute__((objc_direct));
- (NSUInteger)direct:(NSUInteger)arg __attribute__((objc_direct));
@end
@interface CallingConventions(Ext)
+ (NSUInteger)regularExt:(NSUInteger)arg;
- (NSUInteger)regularExt:(NSUInteger)arg;
+ (NSUInteger)directExt:(NSUInteger)arg __attribute__((objc_direct));
- (NSUInteger)directExt:(NSUInteger)arg __attribute__((objc_direct));
@end
__attribute__((objc_runtime_name("CCH")))
__attribute__((swift_name("CCH")))
@interface CallingConventionsHeir : CallingConventions
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,27 @@
#import "direct.h"
#define TEST_METHOD_IMPL(NAME) (NSUInteger)NAME:(NSUInteger)arg { return arg; }
@implementation CallingConventions : NSObject
+ TEST_METHOD_IMPL(regular);
- TEST_METHOD_IMPL(regular);
+ TEST_METHOD_IMPL(direct);
- TEST_METHOD_IMPL(direct);
@end
@implementation CallingConventions(Ext)
+ TEST_METHOD_IMPL(regularExt);
- TEST_METHOD_IMPL(regularExt);
+ TEST_METHOD_IMPL(directExt);
- TEST_METHOD_IMPL(directExt);
@end
@implementation CallingConventionsHeir
@end
@@ -0,0 +1,46 @@
import direct.*
import kotlinx.cinterop.*
import kotlin.test.*
class CallingConventionsNativeHeir() : CallingConventions() {
// nothing
}
typealias CC = CallingConventions
typealias CCH = CallingConventionsHeir
typealias CCN = CallingConventionsNativeHeir
// KT-54610
fun main(args: Array<String>) {
autoreleasepool {
val cc = CC()
val cch = CCH()
val ccn = CCN()
assertEquals(42UL, CC.regular(42))
assertEquals(42UL, cc.regular(42))
assertEquals(42UL, CC.regularExt(42))
assertEquals(42UL, cc.regularExt(42))
assertEquals(42UL, CCH.regular(42))
assertEquals(42UL, cch.regular(42))
assertEquals(42UL, CCH.regularExt(42))
assertEquals(42UL, cch.regularExt(42))
assertEquals(42UL, ccn.regular(42UL))
assertEquals(42UL, ccn.regularExt(42UL))
assertEquals(42UL, CC.direct(42))
assertEquals(42UL, cc.direct(42))
assertEquals(42UL, CC.directExt(42))
assertEquals(42UL, cc.directExt(42))
assertEquals(42UL, CCH.direct(42))
assertEquals(42UL, cch .direct(42))
assertEquals(42UL, CCH.directExt(42))
assertEquals(42UL, cch .directExt(42))
assertEquals(42UL, ccn .direct(42UL))
assertEquals(42UL, ccn .directExt(42UL))
}
}